JQuery - Change certain element classes to specific classes if certain other element is present

不想你离开。 提交于 2019-12-11 04:09:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!