你如何检查 href 链接上的操作?

huangapple go评论59阅读模式
英文:

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&lt;quizzes.size();i++)
{
	%&gt;
	
	   &lt;li&gt;&lt;a href=&quot;start_quizz.jsp&quot;&gt; &lt;%=quizzes.get(i)%&gt;&lt;/a&gt;&lt;/li&gt;
	
	&lt;% 
	
}

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.

&lt;script type=&quot;text/javascript&quot;&gt;
	function show(obj){
		alert(obj.id);
	}
&lt;/script&gt;

where the JSP code is

&lt;% for(int i=0; i&lt;quizzes.size(); i++) { %&gt;
   &lt;li&gt;&lt;a href=&quot;start_quizz.jsp&quot; id=&quot;&lt;%=quizzes.get(i)%&gt;&quot; onclick=&quot;show(this);&quot;&gt; &lt;%=quizzes.get(i)%&gt;&lt;/a&gt;&lt;/li&gt;
&lt;% 
} 
%&gt;

For example, assume your JSP produces the following HTML code:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;Demo&lt;/title&gt;
	&lt;script type=&quot;text/javascript&quot;&gt;
		function show(obj){
			alert(obj.id);
		}
	&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;li&gt;&lt;a href=&quot;start_quizz.jsp&quot; id=&quot;Google&quot; onclick=&quot;show(this);&quot;&gt;Google&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;start_quizz.jsp&quot; id=&quot;Facebook&quot; onclick=&quot;show(this);&quot;&gt;Facebook&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;start_quizz.jsp&quot; id=&quot;Instagram&quot; onclick=&quot;show(this);&quot;&gt;Instagram&lt;/a&gt;&lt;/li&gt;
&lt;/body&gt;
&lt;/html&gt;

If you save this HTML code in an .html file and open it in a browser, you can see this in action.

huangapple
  • 本文由 发表于 2020年8月3日 05:02:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63220995.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定