[英]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.