简体   繁体   English

如何指定特定目标<li>

[英]How to target a specific <li>

I would like to target the following "list" items seperately and amend the href's individually for each. 我想分别定位以下“列表”项,并分别对每个href进行修改。 However, I would like to do it without the "list"'s having a specific div tag/class. 但是,我想在没有“列表”具有特定div标签/类的情况下进行操作。 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: 如果使用jquery,则可以执行以下操作:

$.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
    }
});

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

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