JQuery 切换类父级和子级
我刚刚开始了解 Javascript 和 JQuery,但我无法弄清楚这一点:是否有更短的方法:
$(".toggle").click(function(){
$(this).parent().toggleClass("collapsed");
$(this).parent().children().toggleClass("collapsed");
$(this).parent().next(".abcContent").toggle(0);
我很高兴我终于让它工作了,但肯定这不是它正确完成的方式..我想切换父类(abcTitle),包括它的子类(即按钮)。
<div class="abcTitle">
<div class="abcButton toggle"></div>
<h4 class=toggle>Title</h4><div class="sortable"></div>
</div>
<div class="abcContent">Content</div>
谢谢你给我开导!
为了澄清,这里有一些相关的 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;
}
所以基本上,单击 abcButton 或 H4 应该会触发 abcContent 的折叠,同时将类 .collapsed 添加到 abcButton 和 abcTitle。
(顺便说一句,如果我能弄清楚如何
<div class="sortable"></div>
从单击函数中排除,我可以将 abcTitle 设置为我的单击处理程序,而无需分离 abcButton 和 h4)
I'm just getting to know Javascript and JQuery and I can't figure this out: Is there a shorter way for this:
$(".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 (i.e. button).
<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
.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.
(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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该缓存或链接您的 jQuery 对象;
$(this).parent()
被计算了 3 次。您不需要将
collapsed
类添加到您的子元素中; CSS 应该根据父级具有折叠类的事实来计算出元素是折叠的。您或许可以保证下一个元素始终是
abcContent
,因此您无需担心选择下一个元素。如果您要切换多个元素,最好使用
delegate
或live
方法,它们利用 JavaScript 的事件冒泡机制;事件处理程序仅绑定到一个元素,而不是所有元素,从而提高了性能。综上所述,更好的解决方案可能如下:
You should cache, or chain, your jQuery object;
$(this).parent()
is being calculated 3 times.You shouldn't need to add the
collapsed
class to your children elements; you CSS should work out that the elements are collapsed from the fact the parent has the class of 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.If you are toggling a number of elements, it'll be better to use the
delegate
orlive
methods, which utilize the event bubbling mechanisms of 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: