$(this)和this之间有什么区别? [英] What is the difference between $(this) and this
本文介绍了$(this)和this之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码
$('a').click(function() {
var url= this.href;
alert(url);
});
这很好用,确定返回的结果是标签的网址。
This works just fine and sure enough the returned result is the url of a tag.
但是,如果我将上述代码更改为
However if I change the above code to
$('a').click(function() {
var url= $(this).href;
alert(url);
});
结果未定义。
任何人请帮我解决这个问题?我正在敲打这个...... ....
Anyone please help to clear this out for me? I am banging my head for this ....
推荐答案
$(this)
创建一个包装此
的jQuery对象。本机DOM对象具有 href
属性,但jQuery没有。
$(this)
creates a jQuery object which wraps this
. The native DOM object has an href
attribute, but jQuery does not.
$(这个).attr(href)
可行。
这篇关于$(this)和this之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文