考虑要一个Ajax功能添加到您的网页上诸多环节。为什么jQuery的add()方法似乎不返回一个jQuery对象?
<a href='http://domain/purchase/car' class='purchase'>Car</a>
<a href='http://domain/purchase/bag' class='purchase'>Bag</a>
<a href='http://domain/purchase/laptop' class='purchase'>Laptop</a>
和
<a href='http://domain/sell/car' class='sell'>Car</a>
<a href='http://domain/sell/bag' class='sell'>Bag</a>
<a href='http://domain/sell/laptop' class='sell'>Laptop</a>
现在可以定义一些变量来对这些团体的链接在你的JavaScript代码:
var purchaseLinks = $('.purchase'),
sellLinks= $('.sell');
嗯,够故事)。
根据jQuery's add() documentation,返回值为新的jQuery对象。再次,AMAIK内被附接到一个jQuery对象,this
referes为DOM元素的事件的功能的处理程序。
为什么通过使用add()
方法,this
的处理函数是指document
?我不明白。我无法配合我的知识来形成合乎逻辑的看法。换句话说:
jQueryObject1.click(function(){
// Here, $(this) is the jQuery object
});
jQueryObject2.click(function(){
// Here again, $(this) is the jQuery object
});
jQueryObject1.add(jQueryObject2).click(function(){
// Here $(this) refers to the Document, why?
// I think jQueryObject1.add(jQueryObject2) should equal jQueryObject3
});
UPDATE:
谢谢您的回答。我再次提及读者Live is Deprecated页面,让每个人都可以得到改善。
如果你有兴趣在什么是真正的jQuery内发生的全部细节,我已经更新了我的答案。 – 2012-07-11 12:29:33