在 JavaScript 中获取 href

发布于 2024-11-19 16:47:11 字数 335 浏览 2 评论 0原文

嘿,看在上帝的份上,为什么这会在带有 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉生梦死 2024-11-26 16:47:12
var linkhref = e.target.href;

也有效。

var linkhref = e.target.href;

Works too.

岛歌少女 2024-11-26 16:47:11

$(this) 返回 dom 元素数组,因此 href 属性未定义。要获取 href,您需要使用 jquery attr 方法:

 var link = $(this).attr('href')

在此上下文中的“this”是元素本身,因此它具有可通过以下方式访问的 href 属性:

var  = 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:

 var link = $(this).attr('href')

"this" in this context is the element itself so it has the href property accessible by:

var  = this.href;
半仙 2024-11-26 16:47:11

删除$()。它只是 this.href

Remove the $(). It's just this.href.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文