简体   繁体   English

jQuery toggleClass父级和子级

[英]JQuery toggleClass parent and children

I'm just getting to know Javascript and JQuery and I can't figure this out: Is there a shorter way for this: 我只是了解Javascript和JQuery而无法弄清楚:有没有更短的方法呢?

$(".toggle").click(function(){
$(this).parent().toggleClass("collapsed");
$(this).parent().children().toggleClass("collapsed");
$(this).parent().next(".abcContent").toggle(0);

I'm glad I got it to work after all but surely this isn't how it's done properly... I want to toggle the parent class (abcTitle) including its children (ie button). 我很高兴我能使它工作起来,但是肯定不能正常完成...我想切换父类(abcTitle)及其子元素(即按钮)。

<div class="abcTitle">
<div class="abcButton toggle"></div>
<h4 class=toggle>Title</h4><div class="sortable"></div>
</div>
<div class="abcContent">Content</div>

Thanks for enlightening me! 感谢您的启发!

For clarification here's some relevant CSS 为了澄清起见,这里有一些相关的CSS

.abcButton {
    background-image:url(minus.png);
}
.abcButton.collapsed {
    background-image:url(plus.png); 
}
.abcTitle {
    border-top-left-radius:14px;
}
.abcTitle.collapsed {
    border-bottom-left-radius:14px;
}

So basically a click on either abcButton or H4 is supposed to trigger the collapse of abcContent while adding class .collapsed to both abcButton and abcTitle. 因此,基本上,单击abcButton或H4都应该触发abcContent的崩溃,同时将类.collapsed添加到abcButton和abcTitle中。
(Btw, If I could figure out how to exclude (顺便说一句,如果我能弄清楚如何排除

<div class="sortable"></div>

from the click function I could just make abcTitle my click handler with no need to seperate abcButton and h4) 从click函数中,我可以使abcTitle成为我的点击处理程序,而无需将abcButton和h4分开)

You should cache, or chain, your jQuery object; 您应该缓存或链接jQuery对象; $(this).parent() is being calculated 3 times. $(this).parent()被计算3次。

$(".toggle").click(function(){
    var self = $(this).parent();

    self.toggleClass("collapsed");
    self.children().toggleClass("collapsed");
    self.next(".abcContent").toggle(0);

You shouldn't need to add the collapsed class to your children elements; 您无需将collapsed类添加到子元素中; you CSS should work out that the elements are collapsed from the fact the parent has the class of collapsed. 您的CSS应该从父级具有折叠类的事实中得出元素是折叠的。

div.abcTitle h4 {
    /* Styles when the element is shown */
}

div.abcTitle.collapsed h4 {
    /* Styles when the element is collapsed */
}

You can probably guarantee that the next element will always be the abcContent , so you shouldn't need to worry about selecting the next element. 您可能可以保证下一个元素将始终是abcContent ,因此您不必担心选择下一个元素。

If you are toggling a number of elements, it'll be better to use the delegate or live methods, which utilize the event bubbling mechanisms of JavaScript; 如果要切换许多元素,最好使用delegatelive方法,这些方法利用JavaScript的事件冒泡机制。 the event handler is only bound to one element, rather than all of them, increasing performance. 事件处理程序仅绑定到一个元素,而不是全部绑定到一个元素,从而提高了性能。

Having said all of that, a better solution might be as follows: 综上所述,更好的解决方案可能如下:

$(document).delegate('.toggle', 'click', function () {
    $(this).parent().toggleClass('collapsed').next().toggle();
});
$(document).ready(function(){ 

$(".clickclassname").click(function(){ 

$("body").toggleClass("addclassname"); 
}); 

});

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

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