jquery中的 .end用法详解

1 $("img").clone().appendTo('#showOne').end().addClass('show')

jQuery提供了一个end()命令,在jQuery链内,一旦调用这个方法,方法就会作为返回值退回到前一个wraper.所以后续操作就会应用到前一个wraper.

也就是所.show加到了$("img")上。

又如:

1 <script type="text/javascript">
2 $(function() {
3 $(".column").sortable({
4 connectWith: '.column'
5 });
6
7 $(".portlet").addClass("ui-widget ui-widget-content ui-helper-clearfix ui-corner-all")
8 .find(".portlet-header")
9 .addClass("ui-widget-header ui-corner-all")
10 .prepend('<span class="ui-icon ui-icon-plusthick"></span>')
11 .end() //end 在这里
12   .find(".portlet-content");
13
14 $(".portlet-header .ui-icon").click(function() {
15 $(this).toggleClass("ui-icon-minusthick");
16 $(this).parents(".portlet:first").find(".portlet-content").toggle();
17 });
18
19 $(".column").disableSelection();
20 });
21 </script>

      $(".portlet")返回所有包含有class="portlet"的元素,
然后,在find(".portlet-header")以后,这句的返回就是同时包含class="portlet portlet-header"的,使用end()以后,就退回前一次选择,返回的值又变成所有包含有class="portlet"的元素,简单说就是在一次选择以后再次进行选择使返回值的内容改变以后,退回到上一次的选择,一般用在连写里面。
posted @ 2011-04-08 15:24  blacksheep  阅读(731)  评论(1编辑  收藏  举报