问题描述
在我的网站上,我使用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值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!