18-解决方案
5.4.2 解决方案
将链中的每个方法调用自成一行,在每行的开始放置 .
操作符,然后将链接的每一部分缩进,指出你将要切换到不同元素集的地方。
在使用 .children()
或 .siblings()
之类的方法选择不同元素时增加缩进,在调用 .end()
返回前一个jQuery选择集时减少缩进。
如果你是jQuery新手,应该阅读第1章中有关基本链和 .end()
的秘诀:
$('#box')
addClass('contentBox')
.children(':header')
.addClass('contentTitle')
.click(function() {
$(this).siblings('.contentBody').toggle();
})
.end()
.children(':not(.contentTitle)')
.addClass('contentBody')
.end()
.append('<div class="contentFooter"></div>')
.children('.contentFooter')
.text('generated content');