简体   繁体   中英

jQuery: Adding a class to <a> element

I have a WordPress site and I would like to add a class to the links in the main navigation using jQuery.

This is how the code currently looks:

HTML

<div class="nav-menu">
<ul>
<li class="current_page_item">
<a title="Home" href="www.example.com">Home</a>
</li>
...
</ul>
</div>

And this is what I'd like to achieve:

HTML

<div class="nav-menu">
<ul>
<li class="current_page_item">
<a title="Home" href="www.example.com" class="new-class-goes-here">Home</a>
</li>
...
</ul>
</div>

Does anyone have a clue how to do this?

You can use jQuery's .addClass() method and the appropriate selector for the <a> tags:

$(".nav-menu a").addClass("new-class-goes-here");

Here is a jsFiddle to demonstrate.

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