Jquery: Add rel attribute to tags within all <

2019-05-13 23:56发布

I'm trying to add a rel="lightframe" attribute to all my 'edit' links within my admin_links_node_edit class.

<li class="admin_links_node_edit">
<a href="[link]" title="Edit">Edit</a>
</li>

My code so far looks like this:

$('.admin_links_node_edit a').each(function() {
        $(this).attr('rel','lightframe'); 
});

2条回答
▲ chillily
2楼-- · 2019-05-14 00:18

You don't need to use each(). jQuery's selectors will do it for you :)

$('.admin_links_node_edit a').attr('rel', 'lightframe')

The above code will do the trick.

查看更多
Fickle 薄情
3楼-- · 2019-05-14 00:22

If admin_links_node_edit is reused among other elements, you'll want to specify the element you're working on (li in this case). In addition, as Vijay Dev said, each() isn't needed, as attr() works on every element in the selector. Therefore:

$("li.admin_links_node_edit a").attr('rel','lightframe');
查看更多
登录 后发表回答