jQuery jquery中$(this)和this的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3633270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between $(this) and this in jquery
提问by Sheldon Fernandes
What is the fundamental difference between using $(this) vs this
使用 $(this) 和 this 的根本区别是什么
$('.viewComments').click(function(ev){
//returns the desired value
alert(this.getAttribute('id'));
//Gives an error sayin function is not defined
alert($(this).getAttribute('id'));
//returns the desired value
alert($(this).attr('id'));
});
What I thought was "$(this)" will contain all functions that "this" has and more..But that doesn't seem to be the case.
我认为“$(this)”将包含“this”所具有的所有功能以及更多......但似乎并非如此。
So what exactly is $(this)? and
那么 $(this) 到底是什么?和
Hw do I know what functions are available when I'm using it? (I know I can get them through firebug. but I would like to know if there any some other way- some doc may be)
当我使用它时,我怎么知道有哪些功能可用?(我知道我可以通过萤火虫获得它们。但我想知道是否有其他方法 - 一些文档可能是)
回答by Chris Jester-Young
this
is the DOM object, whereas $(this)
is the jQuery wrapper around same.
this
是 DOM 对象,而$(this)
jQuery 包装器也是如此。
When using this
, you can call DOM methods on it, but not jQuery methods. When using $(this)
, you can call jQuery methods on it, but not DOM methods.
使用时this
,您可以在其上调用 DOM 方法,但不能调用 jQuery 方法。使用时$(this)
,您可以在其上调用 jQuery 方法,但不能调用 DOM 方法。
回答by Pranay Rana
$(this)- represent current DOM element on which event this function is called
$(this)- 表示调用此函数的事件的当前 DOM 元素
The this keyword- In JavaScript this always refers to the “owner” of the function we're executing, or rather, to the object that a function is a method of.
this 关键字- 在 JavaScript 中 this 总是指我们正在执行的函数的“所有者”,或者更确切地说,是指函数作为其方法的对象。
回答by each
In jQuery, this refers to the DOM object, and $(this
) refers to the same object but with jQuery methods added
在 jQuery 中,this 指的是 DOM 对象,而$(this
) 指的是同一个对象,但添加了 jQuery 方法
you can't call this.each()
because each is not a DOM method, its a jquery method
你不能调用,this.each()
因为每个都不是 DOM 方法,它是一个 jquery 方法
you can call $(this).each()
because $(this)
returns a jquery object
你可以调用$(this).each()
因为$(this)
返回一个 jquery 对象
回答by Alek Davis
Here are two articles that you may find helpful:
以下两篇文章可能对您有所帮助:
What is this?by Mike Alsup
这是什么?迈克·阿尔苏普
jQuery's this: demystifiedby Remy Sharp
jQuery 是这样的:Remy Sharp揭开神秘面纱
回答by griegs
$(this) is the current object that was selected using a jQuery selector or event attached to the object.
$(this) 是使用 jQuery 选择器或附加到对象的事件选择的当前对象。
so if you have $('#myelement').click(.....
then $(this)
referes to the element that was clicked on so that $(this).hide()
hides that element.
所以如果你有$('#myelement').click(.....
然后$(this)
引用被点击的元素,以便$(this).hide()
隐藏该元素。
回答by Marco
in jQuery the $() notation is a shorthand for the jQuery selector, so if you say $(this) you are asking jQuery to re-select your object. Then you have the usual jQuery functions available. "this" is the object selected by the outer jQuery call.
在 jQuery 中,$() 符号是 jQuery 选择器的简写,所以如果你说 $(this) 你是在要求 jQuery 重新选择你的对象。然后你就可以使用常用的 jQuery 函数了。“this”是外部 jQuery 调用选择的对象。
回答by Jithi Vasudev
$(this) is a jQuery object and you can use the power and beauty of jQuery, but with 'this' keyword, one need to use native JavaScript.
$(this) 是一个 jQuery 对象,您可以使用 jQuery 的强大功能和美感,但是如果使用 'this' 关键字,则需要使用原生 JavaScript。