简体   繁体   English

在父对象上调用方法之前,从子元素中删除父类(javascript / jquery)

[英]Remove a parent class from a child element before calling method on parent (javascript/jquery)

Say I have the following unordered list 说我有以下无序列表

<ul class="foo">
  <li>
    <div class="bar">
    </div>
    <div class="baz">
    </div> 
 </li>
 <li>
   <div class="bar">
   </div>
   <div class="baz">
   </div> 
 </li>
</ul>
<div class="nav">
    <a class="prev-link">Previous</a>
    <a class="next-link">Next</a>
</div>

I pass the list to a carousel like so $(".foo").carouFredSel(//options) however i don't want the class bar to be affected by the carousel. 我将列表传递给像$(".foo").carouFredSel(//options)这样的轮播,但是我不希望类bar受到轮播的影响。

I've tried $('.bar').removeClass('foo') before calling the carousel, but it doesn't seem to work... it is still part of the carousel. 我在调用轮播之前尝试过$('.bar').removeClass('foo') ,但它似乎不起作用...它仍然是轮播的一部分。 How can i prevent that one div from inheriting the parent class or is there a different way I can prevent that div from being treated as part of the carousel ie (just left in place)? 如何防止该div继承父类,或者有其他方法可以防止将该div视为轮播的一部分,即(留在原地)?

You could try using the !important CSS rule on the .bar class properties you don't want to overwrite. 您可以尝试在不想覆盖的.bar类属性上使用!important CSS规则。 But that's probably not the best use of the rule. 但这可能不是该规则的最佳用途。

You could also try :not() with the Foo class selector. 您也可以使用Foo类选择器尝试:not()。

如果您要这样做,这将从您的整个ul中删除foo类。

$('.bar').parents('.foo').removeClass('foo');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM