有这个
<a href="#">something</a>
然后我在Jquery代码中处理它。这会在悬停时显示浏览器状态栏中的#。这是生产的好方法,还有其他选择吗? 感谢
答案 0 :(得分:3)
您可以使用event.preventDefault()
来停止重定向网页的链接:
HTML:
<a href="http://disneyland.com/">Let's go to DisneyLand!</a>
jQuery的:
$("a").click(function(e){
//jQuery passes in the event object as e
e.preventDefault();
alert("LOL, you really thought you were going?");
});
答案 1 :(得分:0)
您甚至不必定义href属性。通常我会像这样编码:
<a onclick="...">Link</a>
或
$('#blah').click(function(){...});
...
<a id="blah">Link</a>
检查规格:
http://www.w3.org/TR/2003/WD-xhtml2-20030506/mod-hypertext.html
http://www.w3.org/TR/html401/struct/links.html#edef-A
还有更多我肯定,但我认为这实际上取决于您使用的doctype。无论如何长话短说,不指定href属性并且验证器应该传递它是不错的做法。是啊......地址栏中的#总是让我看起来很乱。
答案 2 :(得分:-1)
网址中出现的哈希标记实际上并没有错,但您可能会注意到的另一个效果是您的网页在点击链接后会滚动到顶部。
要解决此问题,请在响应您的活动的函数中使用event.preventDefault()
(假设您正在捕获event
参数):
$(element).click( function(event) {
event.preventDefault();
// your code...
})
或使用return false
:
$(element).click( function(event) {
// your code...
return false;
})