英文:
How can I check the action on a href link?
问题
You can use JavaScript to achieve this. Here's an example of how you can modify your code to check which link was pressed and perform actions accordingly:
for (var i = 0; i < quizzes.length; i++) {
var link = document.createElement("a");
link.href = "start_quizz.jsp";
link.innerHTML = quizzes[i];
link.addEventListener("click", function(event) {
event.preventDefault(); // Prevent the default link behavior
var clickedLink = this.innerHTML;
// Check which link was pressed and perform actions
if (clickedLink === quizzes[0]) {
alert("You pressed the first link.");
// Add more actions here for the first link
} else if (clickedLink === quizzes[1]) {
// Add actions for the second link
}
// Add more conditions for other links as needed
});
var listItem = document.createElement("li");
listItem.appendChild(link);
document.getElementById("yourList").appendChild(listItem);
}
Make sure to replace "yourList"
with the ID of the <ul>
element where you want to append these links. This JavaScript code will check which link was pressed and execute specific actions for each link.
英文:
How can I check the action on a href link when I create them in a for loop I want to check which one I pressed on and to give some actions for that link:
E.g.:
for(int i=0;i<quizzes.size();i++)
{
%>
<li><a href="start_quizz.jsp"> <%=quizzes.get(i)%></a></li>
<%
}
Let's say I press on the first link and I want the page "start_quizz.jsp" to check which link I pressed and print a message if I pressed the first link.
答案1
得分: 0
以下是您要翻译的内容:
您可以使用JavaScript代码来获取被点击链接的ID,例如:
<script type="text/javascript">
function show(obj){
alert(obj.id);
}
</script>
其中JSP代码为:
<% for(int i=0; i<quizzes.size(); i++) { %>
<li><a href="start_quizz.jsp" id="<%=quizzes.get(i)%>" onclick="show(this);"><%=quizzes.get(i)%></a></li>
<%
}
%>
例如,假设您的JSP生成以下HTML代码:
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<script type="text/javascript">
function show(obj){
alert(obj.id);
}
</script>
</head>
<body>
<li><a href="start_quizz.jsp" id="Google" onclick="show(this);">Google</a></li>
<li><a href="start_quizz.jsp" id="Facebook" onclick="show(this);">Facebook</a></li>
<li><a href="start_quizz.jsp" id="Instagram" onclick="show(this);">Instagram</a></li>
</body>
</html>
如果您将此HTML代码保存在一个.html
文件中并在浏览器中打开它,您可以看到它的效果。
英文:
You can use JavaScript code to tell the id of the link clicked e.g.
<script type="text/javascript">
function show(obj){
alert(obj.id);
}
</script>
where the JSP code is
<% for(int i=0; i<quizzes.size(); i++) { %>
<li><a href="start_quizz.jsp" id="<%=quizzes.get(i)%>" onclick="show(this);"> <%=quizzes.get(i)%></a></li>
<%
}
%>
For example, assume your JSP produces the following HTML code:
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<script type="text/javascript">
function show(obj){
alert(obj.id);
}
</script>
</head>
<body>
<li><a href="start_quizz.jsp" id="Google" onclick="show(this);">Google</a></li>
<li><a href="start_quizz.jsp" id="Facebook" onclick="show(this);">Facebook</a></li>
<li><a href="start_quizz.jsp" id="Instagram" onclick="show(this);">Instagram</a></li>
</body>
</html>
If you save this HTML code in an .html
file and open it in a browser, you can see this in action.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论