如何使用 Jquery 删除“href”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1688084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove "href" with Jquery?
提问by Steven
<a id="a$id" onclick="check($id,1)" href="javascript:void(0)" class="black">Qualify</a>
After "href" is removed, is "Qualify" still clickable?
去掉“href”后,“Qualify”还能点击吗?
回答by Langdon
Your title question and your example are completely different. I'll start by answering the title question:
您的标题问题和您的示例完全不同。我将首先回答标题问题:
$("a").removeAttr("href");
And as far as not requiring an href, the generally accepted way of doing this is:
至于不需要href,普遍接受的做法是:
<a href"#" onclick="doWork(); return false;">link</a>
The return false is necessary so that the href doesn't actually go anywhere.
返回 false 是必要的,以便 href 实际上不会去任何地方。
回答by Brad Parks
If you want your anchor to still appear to be clickable:
如果您希望您的锚点仍然可以点击:
$("a").removeAttr("href").css("cursor","pointer");
And if you wanted to remove the href from only anchors with certain attributes (eg ones that just have a hash mark as the href - this can be useful in asp.net)
如果您只想从具有某些属性的锚点中删除 href(例如,那些只有哈希标记作为 href 的锚点 - 这在 asp.net 中很有用)
$("a[href='#']").removeAttr("href").css("cursor","pointer");
回答by bryceadams
If you wanted to remove the href, change the cursor and also prevent clicking on it, this should work:
如果您想删除 href,更改光标并防止点击它,这应该有效:
$("a").attr('href', '').css({'cursor': 'pointer', 'pointer-events' : 'none'});
$("a").attr('href', '').css({'cursor': 'pointer', 'pointer-events' : 'none'});