jquery中如何通过href获取元素?

发布于 2024-09-06 23:10:16 字数 52 浏览 5 评论 0原文

我想通过 jquery 或 javascript 中的 href 属性获取元素。这可能吗?

I want to get an element by its href attribute in jquery or javascript. Is that possible?

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

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

发布评论

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

评论(4

七度光 2024-09-13 23:10:16

是的,您可以使用 jQuery 的 属性选择器 来实现。

var linksToGoogle = $('a[href="https://google.com"]');

或者,如果您感兴趣的是以某个 URL 开头的链接,请使用 attribute-starts-with 选择器:

var allLinksToGoogle = $('a[href^="https://google.com"]');

Yes, you can use jQuery's attribute selector for that.

var linksToGoogle = $('a[href="https://google.com"]');

Alternatively, if your interest is rather links starting with a certain URL, use the attribute-starts-with selector:

var allLinksToGoogle = $('a[href^="https://google.com"]');
情深如许 2024-09-13 23:10:16

如果您想获取 href 属性中包含 URL 一部分的任何元素,您可以使用:

$( 'a[href*="google.com"]' );

这将选择 href 包含 google.com 的所有元素,例如:

正如 @BalusC 在下面的评论中所述,它还会匹配 href 中任何位置具有 google.com 的元素,例如 blahgoogle.com

If you want to get any element that has part of a URL in their href attribute you could use:

$( 'a[href*="google.com"]' );

This will select all elements with a href that contains google.com, for example:

As stated by @BalusC in the comments below, it will also match elements that have google.com at any position in the href, like blahgoogle.com.

不即不离 2024-09-13 23:10:16
var myElement = $("a[href='http://www.stackoverflow.com']");

http://api.jquery.com/attribute-equals-selector/

var myElement = $("a[href='http://www.stackoverflow.com']");

http://api.jquery.com/attribute-equals-selector/

命硬 2024-09-13 23:10:16

是的。

$('a[href=\'http://google.com\']')

http://api.jquery.com/attribute-equals-selector/

Yes.

$('a[href=\'http://google.com\']')

http://api.jquery.com/attribute-equals-selector/

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