end()函数

时间:2020-03-06 15:03:43  来源:igfitidea点击:

jQuery中的end()函数将元素集还原为上次破坏性更改之前的值,因此我可以看到应如何使用它,但是我已经看到了一些代码示例,例如:on alistapart(可能来自旧版本的jQuery(该文章来自2006年),该注释以.end()结尾。例如:

$( 'form.cmxform' ).hide().end();
  • 这有什么作用吗?
  • 这是我也应该做的事情吗?
  • 上面的代码还返回什么?

解决方案

end()什么也没做。这样的编码是没有意义的。它会返回$('#myBox')-这个例子很差。更有趣的是这样的:

$('#myBox').show ().children ('.myClass').hide ().end ().blink ();

这将显示" 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?');
    });