gpt4 book ai didi

javascript - 从绝对 url 字符串中获取 anchor 值

转载 作者:行者123 更新时间:2023-11-30 07:10:38 24 4
gpt4 key购买 nike

我正在使用这个 jQuery 选择器:

$("a[href*='#']").click(function(e) {
e.preventDefault();
alert(#valueinhere)
});

选择链接到 anchor 的链接

我的链接是绝对链接和相对链接,因此它们看起来像 http://www.myweb.com/test#anchortest#anchor# anchor

如何从上面的所有链接中获取字符串#anchor?是否有任何正则表达式或类似的东西?

(我不能使用 window.location.hash 因为 e.preventDefault() 是必需的)

最佳答案

为什么不能从 anchor 元素中获取hash

this.hash

它与您是否阻止点击事件的默认行为无关。

$("a[href*='#']").click(function(e) {
e.preventDefault();
alert(this.hash);
});

这是一个有效的 example .

关于javascript - 从绝对 url 字符串中获取 anchor 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8683474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com