简体   繁体   中英

How to differentiate two links with the same content using jQuery?

I have a page that contains two links with the same text "Add new item", but are targeting different URLs.

I created a javascript that uses jQuery library which references the link by its text. The code is:

var anchorElement = $("a:contains('Add new item')");

This is fine when I want to reference the first link. But, how do I reference the second one, being that they have the same text? Thanks.

To loop through use this

$("a:contains('Add new item')").each(function(){
    alert($(this).attr('href'));
});

You can also grab them by index

var first = $("a:contains('Add new item')").eq(0);
var second = $("a:contains('Add new item')").eq(1);

尝试搜索过滤器,或者使用each()函数对其进行迭代。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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