使用 jquery 取消 href 不起作用
我正在尝试删除指向确定类的链接,以便改为执行Javascript(onclick之类的东西)(如果是的话,则启用了javascript。这就是为什么我想将href保留在源代码中..任何方式,我正在尝试:
<script type="text/javascript">
$(document).ready(function() {
$('.remove').attr('href', '#');
});
</script>
但它没有:S
I'm trying to remove the links to determinated class of so the Javascript it's executed instead ( onclick and things like that ) (and if of corse, javascript it's enabled. that's why i want to keep the href in the source code.. any way, i'm trying with:
<script type="text/javascript">
$(document).ready(function() {
$('.remove').attr('href', '#');
});
</script>
but it doesn't :S
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个...
将
href
设置为空白片段#
是不好的做法,很糟糕而且丑陋。它经常使浏览器跳转到页面顶部。或者,使用您的点击处理程序,您可以执行...
您还可以
返回 false
。Try this instead...
Setting the
href
to a blank fragment#
is bad practice, hacky and ugly. It often makes the browser jump to the top of the page.Alternatively, with your click handlers you could do...
You can also
return false
.我有点理解你想要做什么,正确的方法不是删除 # href ,我不建议返回 false,因为你将无法传递 jQuery 对象:
I kind of understand what your trying to do and the right way to go about it is not removing the # href , i would not advise on returning false because you wont be able to pass the jQuery object:
只需添加推荐的 event.preventDefault() 作为事件处理程序的最后一行。
您不需要更改 href 值。
根据您的代码:
Just add the recommended event.preventDefault() as the last line for your event handler.
You don't event need to change the href value.
Base on your code:
为了使它更简单,只需使用 HTML:
如果你想做一个 JS,带有函数
你的 HTML 应该
现在来调用该函数
To make it more easy, just using an HTML:
If you want to do a JS, with function
Your HTML should be
Now to call for the function