使用$(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'));
});
Run Code Online (Sandbox Code Playgroud)
我认为"$(this)"将包含"this"具有的所有功能以及更多..但似乎并非如此.
那么究竟是什么(这个)?和
我在使用它时知道哪些功能可用吗?(我知道我可以通过萤火虫来获取它们.但我想知道是否有其他方式 - 某些文档可能是)
Chr*_*ung 146
this是DOM对象,而$(this)jQuery包装器是相同的.
使用时this,可以在其上调用DOM方法,但不能调用jQuery方法.使用时$(this),可以在其上调用jQuery方法,但不能调用DOM方法.
$(this) - 表示调用此函数的事件的当前DOM元素
this关键字 - 在JavaScript中,它总是指我们正在执行的函数的"所有者",或者更确切地说,指向函数是其方法的对象.