获取用 javascript 编写的 href 属性

发布于 2024-11-25 09:22:26 字数 221 浏览 2 评论 0原文

我想获得一个 href 属性,因为它出现在 html 中 - 而不是完全限定的 url。

<a href="foo.html">foo</a>

$('a[href]').each(function(){
   alert(this.href);
});

获取以 http:// 开头的绝对 URL。我希望它能得到“foo.html”。

I would like to get an href attribute as it appears in the html - instead of a fully qualified url.

<a href="foo.html">foo</a>

$('a[href]').each(function(){
   alert(this.href);
});

Gets an absolute url starting with http:// . I'd like it to get "foo.html" instead.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

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

评论(2

木槿暧夏七纪年 2024-12-02 09:22:26

尝试 .attr('href') 获取 href 值。

Try .attr('href') to get the href value.

凉风有信 2024-12-02 09:22:26

您需要更改元素的循环方式以及使用 this 的方式:

$('a').each(function(){
   alert( $( this ).attr( 'href' ) );
});

这里是工作示例。

You will need to alter the way you loop through the element, and the way you use this:

$('a').each(function(){
   alert( $( this ).attr( 'href' ) );
});

Working example here.

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