简体   繁体   English

提醒用户单击的链接的ID

[英]alert the ID of the Link the User Clicks On

I have a couple links that each have unique IDs. 我有几个链接,每个链接都有唯一的ID。 I want to call a single function that when anyone of the links is clicked, the user gets an alert that says the link ID. 我想调用单个函数,当单击任何链接时,用户会收到一条说明链接ID的警报。 I am stumped. 我很难过。 Here's what I have. 这就是我所拥有的。 I want to do this with pure JavaScript. 我想用纯JavaScript做到这一点。

My code: 我的代码:

<p><a href="#" id="link0">Link0</a></p>
<p><a href="#" id="link1">Link1</a></p>
<p><a href="#" id="link2">Link2</a></p>
<p><a href="#" id="link3">Link3</a></p>

<script>
function test(i) {
    alert("You just clicked on link:" + i); 
}

onclick = function () { 
    test(i);
};
</script>

Try this 尝试这个

var links = document.getElementsByTagName( "a" );

for ( var i = 0; i < links.length; i++ ) {
    links[i].addEventListener( "click", function( e ) {
        e.preventDefault();
        alert( this.id );
    }, false );
}

DEMO DEMO

I just created an example for you using jQuery. 我刚刚使用jQuery为您创建了一个示例。

$(document).ready(function() {
    $("a").click(function() {
       alert(this.id); 
    });
});

http://jsfiddle.net/Yynw9/ http://jsfiddle.net/Yynw9/

To use jQuery you had to include the library in your header: 要使用jQuery,您必须在头文件中包含库:

<script src="//code.jquery.com/jquery-1.9.1.js" type="text/javascript" />

Here's the javascript you are looking for: 这是您要找的javascript:

var elements = document.getElementsByTagName("a");
for(var i = 0, length = elements.length; i < length; i++) {
    elements[i].addEventListener( "click", function(e) {
        e.preventDefault();
        alert("You just clicked on link: " + this.id); 
    }, false );
}

JSFiddle: http://jsfiddle.net/3R4FA/1/ JSFiddle: http//jsfiddle.net/3R4FA/1/

The code loops first selects all your elements that are <a> tags. 代码循环首先选择所有<a>标签的元素。 It then loops through this array, and adds a click event listener to each item. 然后循环遍历此数组,并为每个项添加一个click事件侦听器。 The use of the this keyword accesses the element that triggered the event, and then you can fetch the id. 使用this关键字可以访问触发事件的元素,然后您可以获取id。

in browsers that support it, you can use querySelectorAll to grab all of the ids which begin with 'link' or you can add a class and use getElementsByClassName.. in legacy browsers, the solution is not as clean, but you can grab all of the 'a' tags in the document or inside of an element, and check if the 'id' attribute starts with 'link'... 在支持它的浏览器中,你可以使用querySelectorAll来获取所有以'link'开头的id,或者你可以在旧版浏览器中添加一个类并使用getElementsByClassName ..解决方案不是很干净,但你可以抓住所有文档中或元素内的'a'标签,并检查'​​id'属性是否以'link'开头...

function test(i) {
    alert("You just clicked on link:" + this.id); 
}
if (document.querySelectorAll('a[id^="link"]')) {
    var elements = document.querySelectorAll('a[id^="link"]');
    var i = 0;
    for (i = 0; i < elements.length; i += 1) {
        elements[i].onclick = test;
    }
} else {
    var elements = document.getElementsByTagName('a');
    var i = 0;
    for (i = 0; i < elements.length; i += 1) {
        if (elements[i].id.substr(0,4) === 'link') {
            elements[i].onclick = test;
        }
    }
}

http://jsfiddle.net/Xvwme/3/ http://jsfiddle.net/Xvwme/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM