问题
Hi there I'm trying to code a blog theme which will have tags etc so wat I'd like is for certain classes of mostly parent divs to change when tagged with something (example below).
classes are spans (using bootstrap)
html
<div id="a" class="span6">
</div>
<div id="b" class="span6">
<a class="taglink">[tag]</a>
</div>
What I'd like is for div a and b's classes to change to say span12, and only them and no other parent/adjacent/child etc divs.
This is so that when I tag the post with certain tags I'd like some of its layout properties to change (via its css class)
回答1:
Make a listener for the tags so that it will call the following
$('#a,#b').addClass('span12').removeClass('span6');
I hope this will help
If you want anything else for the listener please show more code
回答2:
This will select the exact elements you want and swap the classes:
$(".taglink").click(function(){
$(this).parent("#b").removeClass('span6').addClass('span12').prev("#a").removeClass('span6').addClass('span12');
});
Demo: http://jsfiddle.net/rniestroj/aKK3c/
回答3:
Create an event listener for when the content of the a.taglink changes, then use the following:
$('#a.span6,#b.span6').removeClass('span6').addClass('span12');
来源:https://stackoverflow.com/questions/14654387/jquery-change-certain-element-classes-to-specific-classes-if-certain-other-ele