在 JavaScript 中获取 href
嘿,看在上帝的份上,为什么这会在带有 href 的 标签上返回“未定义”?
function ajax(){
$('a').bind('click', function(e){
e.preventDefault();
var linkhref = $(this).href;
alert (linkhref);
});
}
$(document).ready(function(){
ajax();
})
我只是不明白:p。非常感谢你们的帮助:)
Hey why in the name of god does this return "undefined" on an <a>
tag with an href?
function ajax(){
$('a').bind('click', function(e){
e.preventDefault();
var linkhref = $(this).href;
alert (linkhref);
});
}
$(document).ready(function(){
ajax();
})
I just don't get it :p. Thanks a lot for your help guys :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也有效。
Works too.
$(this) 返回 dom 元素数组,因此 href 属性未定义。要获取 href,您需要使用 jquery attr 方法:
在此上下文中的“this”是元素本身,因此它具有可通过以下方式访问的 href 属性:
$(this) returns an array of dom elements hence href property is not defined. To get href you need to use a jquery attr method:
"this" in this context is the element itself so it has the href property accessible by:
删除
$()
。它只是this.href
。Remove the
$()
. It's justthis.href
.