javascript 如何使用javascript获取onclick链接的ID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20233893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get the id of a link onclick using javascript?
提问by user3034828
Hi may i know how can i get my id of my links using javascript? the scenario was i have 3 links i want to get the element by tagname? is it possible?
嗨,我可以知道如何使用 javascript 获取我的链接的 id 吗?场景是我有 3 个链接,我想通过标记名获取元素?是否可以?
<a href="docview.php?id=25" id="1">July 3, 2013</a>
<a href="docview.php?id=26" id="2">July 10, 2013</a>
<a href="docview.php?id=27" id="3">July 17, 2013</a>
I want to display the ID of my link the one i chose and then when i click n 2nd link the id of the second link will display. tahnks
我想显示我选择的链接的 ID,然后当我单击第二个链接时,将显示第二个链接的 ID。任务
回答by suneesh
You can use jQuery
你可以使用 jQuery
$("a").click(function(){
alert(this.id);
});
回答by Enam
Pure JS:
纯JS:
var elements = document.querySelectorAll("a");
for (var i = 0; i < elements.length; i++)
{
elements[i].addEventListener("click", function() {
console.log(this.id)
}, true);
}
As far as I know querySelectorAll
is faster than getElementsByTagName
or either JQuery selector.
据我所知,querySelectorAll
它比getElementsByTagName
或 JQuery 选择器都快。
回答by Ankit Tyagi
var anchor = document.getElementsByTagName('a');
for(var i=0;i<anchor.length;i++){
anchor[i].addEventListener("click", function() {
alert(this.id);
return false;
}
}
回答by Theo Anderson
You can try the following: the function is called when the link is clicked(you can also use other events such as onmouseover) it passes the id of the element to the function and prints it to the screen. Hope this helps.
您可以尝试以下操作:单击链接时调用该函数(您也可以使用其他事件,例如 onmouseover),它将元素的 id 传递给该函数并将其打印到屏幕上。希望这可以帮助。
<!DOCTYPE html>
<html>
<body>
<a href="docview.php?id=25" id="1" onclick="myFunction('1')">July 3, 2013</a>
<a href="docview.php?id=26" id="2" onclick="myFunction('2')">July 10, 2013</a>
<a href="docview.php?id=27" id="3" onclick="myFunction('3')">July 17, 2013</a>
<script>
function myFunction(id) {
var x=document.getElementById(id);
document.write(x.id);
}
</script>
</body>
</html>
回答by Theo Anderson
Here is the code for changing the value of the label...
这是更改标签值的代码...
<!DOCTYPE html>
<html>
<body>
<a href="docview.php?id=25" id="1" onmouseover="myFunction('1')">July 3, 2013</a>
<a href="docview.php?id=26" id="2" onmouseover="myFunction('2')">July 10, 2013</a>
<a href="docview.php?id=27" id="3" onmouseover="myFunction('3')">July 17, 2013</a>
<script>
var newString = "this is the new string";
function myFunction(id) {
document.getElementById(id).innerHTML = newString;
}
</script>
</body>
</html>
the innerHTML property appends the current text to whatever you specify. Also, you would change the event from onmouseover to whatever suits your needs. Hope this helps.
innerHTML 属性将当前文本附加到您指定的任何内容。此外,您可以将事件从 onmouseover 更改为适合您需要的任何事件。希望这可以帮助。