简体   繁体   中英

How to target a specific <li>

I would like to target the following "list" items seperately and amend the href's individually for each. However, I would like to do it without the "list"'s having a specific div tag/class. Is it possible?

<li><a href="javascript:;">Category 1</a></li>
<li><a href="javascript:;">Category 2</a></li>
<li><a href="javascript:;">Category 3</a></li>

If using jquery, you can do something like this:

$.each($("li a"), function(){
     var href = $(this).attr('href');
     var text = $(this).text();
     //now you have your href and text
     if(text == 'Category 1'){
        //updated to change the link to a different URL
        $(this).attr('href', 'http://www.your-new-link');
     }
     if(text == 'Category 2'){
        //do something else
    }
});

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