jQuery 超链接 - href 值? [英] jQuery hyperlinks - href value?

查看:25
本文介绍了jQuery 超链接 - href 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站上,我使用 jQuery 来挂钩元素的事件,即超链接.由于这些超链接只在当前页面上执行操作,并没有引导到任何地方,所以我一直将#"的href属性放在:

On my website I use jQuery to hook the events of elements, namely hyperlinks. As these hyperlinks only perform actions on the current page, and do not lead anywhere, I have been putting a href attribute of "#" in:

<a href="#">My Link</a>

然而,在某些浏览器中,这会导致页面从右滚动到顶部,这显然是不受欢迎的行为.我试过使用一个空白的 href 值,或者不包括一个,但是鼠标悬停时不会变成手形光标.

However in some browsers this causes the page to scroll right to top which is obviously undesirable behaviour. I've tried using a blank href value, or not including one, but then the mouse does not change to the hand cursor upon hovering.

我应该在里面放什么?

推荐答案

$('a').click(function (event) 
{ 
     event.preventDefault(); 
     //here you can also do all sort of things 
});

然后你可以在每个 href 中放入任何你想要的东西,jQuery 将触发 preventDefault() 方法并且你不会被重定向到那个地方.

Then you can put in every href whatever you want and jQuery will trigger the preventDefault() method and you will not be redirected to that place.

这篇关于jQuery 超链接 - href 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆