当前位置:嗨网首页>书籍在线阅读

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');