end()函数

nic*_*ckf 4 jquery

end()jQuery中的函数将元素集恢复为最后一次破坏性更改之前的内容,因此我可以看到它应该如何使用,但我已经看到了一些代码示例,例如:on alistapart (可能来自旧版本) jQuery的文章 - 这篇文章是从2006年开始的,它完成了每个语句.end().例如:

$( 'form.cmxform' ).hide().end();
Run Code Online (Sandbox Code Playgroud)
  • 这有什么影响吗?
  • 这是我应该做的吗?
  • 上面的代码甚至返回了什么?

Joh*_*kin 5

end()没有任何作用.这样的编码是没有意义的.它将返回$('#myBox')- 示例非常差.更有趣的是这样的:

$('#myBox').show ().children ('.myClass').hide ().end ().blink ();
Run Code Online (Sandbox Code Playgroud)

将显示myBox,隐藏指定的孩子,然后闪烁框.这里有更多有趣的例子:

http://simonwillison.net/2007/Aug/15/jquery/

如:

$('form#login')
    // hide all the labels inside the form with the 'optional' class
    .find('label.optional').hide().end()
    // add a red border to any password fields in the form
    .find('input:password').css('border', '1px solid red').end()
    // add a submit handler to the form
    .submit(function(){
        return confirm('Are you sure you want to submit?');
    });
Run Code Online (Sandbox Code Playgroud)