簡體   English   中英

如何指定特定目標<li>

[英]How to target a specific <li>

我想分別定位以下“列表”項,並分別對每個href進行修改。 但是,我想在沒有“列表”具有特定div標簽/類的情況下進行操作。 可能嗎?

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

如果使用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