简体   繁体   中英

JQuery change class name inside of specific elements

I am looking for JQuery script to change a tag class inside of specific tags.

For example,

<h4 class=" id_1">
    <span class="favorite"> </span>
</h4>

In above tags, I want to change class="favorite" to class="title" by id_1 class. CSS for favorite and title is different.

Thank you!

You can also use attr method:

$('.id_1 .favorite').attr('class','title');

Using JavaScript:

document.querySelector('.id_1 .favorite').setAttribute('class','title');

您可以使用addClassremoveClass

$(".id_1").removeClass("favourite").addClass("title");

You can use addClass and removeClass to achieve this:

$('.id_1 .favorite').removeClass('favorite').addClass('title');

You could also use toggleClass if you can guarantee the starting state of the relevant elements:

$('.id_1 .favorite').toggleClass('favorite title');
$('.id_1 .favorite').removeClass('favourite').addClass('title');

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