jQuery中的$(this)和this之间的区别 [英] Difference between $(this) and this in jquery
问题描述
使用$(this)与this之间的根本区别是什么
What is the fundamental difference between using $(this) vs 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'));
});
我以为"$(this)"将包含"this"具有的所有功能以及更多功能.但是,情况似乎并非如此.
What I thought was "$(this)" will contain all functions that "this" has and more..But that doesn't seem to be the case.
那么,$(this)到底是什么?和
So what exactly is $(this)? and
为什么我知道使用它们时可以使用哪些功能? (我知道我可以通过firebug来获取它们.但是我想知道是否还有其他方法-可能是一些文档)
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)
推荐答案
this
是DOM对象,而$(this)
是围绕它的jQuery包装器.
this
is the DOM object, whereas $(this)
is the jQuery wrapper around same.
使用this
时,可以在其上调用DOM方法,但不能调用jQuery方法.使用$(this)
时,可以在其上调用jQuery方法,但不能在DOM方法上调用.
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.
这篇关于jQuery中的$(this)和this之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!