这对vs(这个) [英] this vs $(this)
问题描述
可能重复:
jQuery $(this)vs this
我是新来的这个并试图让我的概念正确。有许多使用 this
和 $(this)
的情况。有人可以解释一下我们使用两个不同的这个的区别和条件吗?
I'm new to this and trying to get my concept right. There has been many instances of the use of "this
" and "$(this)
". Can someone please explain the difference and in what condition that we use the two different "this"?
推荐答案
在jQuery函数中,这个
通常是指你正在处理的实际DOM元素,而 $(this)
返回一个jQuery对象,包装元素。
In jQuery functions, this
most often refers to the actual DOM element you're dealing with, whereas $(this)
returns a jQuery object that wraps the element.
在JavaScript中,此
始终引用当前范围。 jQuery的许多函数都会将该范围设置为您正在使用的元素。
In JavaScript, this
always refers to the current scope. Many of jQuery's functions will set that scope to be the element you're working with.
例如
$("#someElement").click(function() {
this; // the element itself
$(this); // a jQuery wrapper-object around the element
});
重点是,jQuery对象具有所有jQuery函数(如 .detatch()
或 .prependTo()
等),而DOM元素是浏览器提供的。在上面的示例中,如果您调用 document.getElementById(someElement)
The point is, that the jQuery object has all the jQuery functions (like .detatch()
or .prependTo()
etc.), while the DOM element is what the browser provides. In the example above, the element would be exactly the same as what you'd get, if you called document.getElementById("someElement")
这篇关于这对vs(这个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!