.end()函数在jQuery中做什么? [英] What does the .end() function do in jQuery?

查看:71
本文介绍了.end()函数在jQuery中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了此网页 http://api.jquery.com/end/但是我对.end()的实际用途仍然一无所知.它是做什么用的,怎么使用?

I have already read this web page http://api.jquery.com/end/ but I am still clueless on what .end() actually does. What is it for and how do you use it?

我也在读一本jQuery的书,但它在.end()上略显釉面,并且没有给出其用途的任何示例.有人可以澄清吗?

I am also reading a jQuery book but it lightly glazes over .end() and does not give any examples of what it's for. Can someone clarify?

推荐答案

$("body").find("span").css("border", "2px red solid");

vs

$("body").find("span").end().css("border", "2px red solid");

在此确切页面上的Firebug控制台中分别执行这些语句,并注意行为的不同.基本上,.end()告诉它在找到所有跨度之后返回到正文,然后将边框应用于正文,而不是跨度.如果那里没有.end(),则jQuery代码基本上会正常运行,并将.css()应用于正文中的span元素.

Execute these statements separately in Firebug console on this exact page, and notice how different the behaviors are. Basically, .end() tells it to go back to body after finding all spans, and apply the border to body, not the spans. If we don't have the .end() there, the jQuery code basically behaves normally and applies the .css() to our span elements inside of body.

BODY > SPAN > APPLY BORDER TO SPANS

end()一起变为

BODY > SPAN > GO BACK TO BODY > APPLY BORDER TO BODY

find()是一种破坏性操作,这意味着它将更改jquery对象数组内部的元素.

The find() is a destructive operation, meaning it changes what elements are inside of your jquery objects array.

$('body') 

我们当前的元素是身体

$('body').find('span') 

我们使用了破坏性操作find(),该操作将整个对象集合更改为在主体内部填充跨度,主体不再在集合中

we used a destructive operation find() which changes our entire objects collection to be populated with spans inside of body, body is no longer in the collection

$('body').find('span').end() 

因为find是一个破坏性"的操作,它会在执行.find()之前恢复到原来的状态,或者说ctrl-Z是最后一次更改了我们的jquery集合的事情.

because find is a "destructive" operation it reverts back to before we did .find(), basically un-does or ctrl-Z's the last thing that changed our jquery collection.

这篇关于.end()函数在jQuery中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆