如何停止主题标签的默认点击行为?
可能的重复:
使用 jquery 停止默认主题标签行为
我在加载的链接上有一些 jquery 代码使用 .load() 将 DIV 添加到页面中,它还会在 url 中添加主题标签以用于分页/书签目的;我想要做的是当点击链接时阻止页面跳转到div。
$('#jqNav li a').click(function(){
if($(this).parent().is(".nav1")){ $('.landing .main .nav ul').css({ "background-position" : "0 -50px" });}
else if($(this).parent().is(".nav2")) { $('.landing .main .nav ul').css({ "background-position" : "0 -100px" });}
else if($(this).parent().is(".nav3")) { $('.landing .main .nav ul').css({ "background-position" : "0 -150px" });}
else if($(this).parent().is(".nav4")) { $('.landing .main .nav ul').css({ "background-position" : "0 -200px" });};
stopAnim = true;
$page = $(this).attr('href');
var $hashTag = $(this).attr('name');
window.location = "#" + $hashTag;
loadData();
e.preventdefault();
return false;
});
Possible Duplicate:
Stop default hashtag behavior with jquery
I have some jquery code on a link that loads a DIV into the page using .load(), it also adds a hashtag to the url for pagination / bookmarking purposes; what i want to do is to stop the page from jumping to the div when the link is clicked.
$('#jqNav li a').click(function(){
if($(this).parent().is(".nav1")){ $('.landing .main .nav ul').css({ "background-position" : "0 -50px" });}
else if($(this).parent().is(".nav2")) { $('.landing .main .nav ul').css({ "background-position" : "0 -100px" });}
else if($(this).parent().is(".nav3")) { $('.landing .main .nav ul').css({ "background-position" : "0 -150px" });}
else if($(this).parent().is(".nav4")) { $('.landing .main .nav ul').css({ "background-position" : "0 -200px" });};
stopAnim = true;
$page = $(this).attr('href');
var $hashTag = $(this).attr('name');
window.location = "#" + $hashTag;
loadData();
e.preventdefault();
return false;
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试在链接上使用 use
e.preventDefault();
点击Try using
$(window).scrollTop(0);
来阻止#
以免影响页面Try using use
e.preventDefault();
on the link clickTry using
$(window).scrollTop(0);
to prevent the#
from affecting the page您可以为此
http://api 使用
e.preventDefault()
。 jquery.com/event.preventDefault/或者使用:
preventDefault()
和stopPropagation()
http://api.jquery.com/event.stopPropagation/
编辑
不要认为你想要的都可以做到。因为如果能够在不访问该 URL 的情况下更改页面的 URL,那么安全性会很差。
例如,您可以将 URL 更改为银行的 URL,让用户认为您的页面代表银行。
也许它只在添加散列时起作用,所以你可以尝试以下操作(没有测试过,可能很行不通):
EDIT2
我知道我只是告诉你它不起作用,但我只是在这里偶然发现了这篇文章在使用 HTML5 来做你想做的事的SO上:
有一个更改浏览器地址栏而不刷新页面的方法?
再次未测试:)
You can use
e.preventDefault()
for thishttp://api.jquery.com/event.preventDefault/
Or use:
Which does both
preventDefault()
andstopPropagation()
http://api.jquery.com/event.stopPropagation/
EDIT
Don't think what you want can be done. Since it would be bad security wise to be able to change the URL of a page without going to that URL.
You can for example change the URL to the URL of a bank letting users think your page represent the bank.
Maybe it works when only adding a hash so you can give the following a try (haven't tested it, might very well not work):
EDIT2
I know I just told you it doesn't work however I just stumbled upon this post here on SO which uses HTML5 to do what you want:
Is there a way to change the browser's address bar without refreshing the page?
Again not tested :)
您可以使用
return false;
作为点击处理程序中的最后一条语句。或者同时使用event.preventDefault()
和event.stopPropagation()
(return false
等于两者一起使用,防止该元素的默认行为并且阻止事件通过 DOM 向上冒泡)。参考文献:
event.preventDefault()
event.stopPropagation()
You could use
return false;
as the last statement in the click-handler. That or use bothevent.preventDefault()
andevent.stopPropagation()
(return false
is equal to the both of them together, preventing the default behaviour for that element and preventing the event from bubbling up through the DOM).References:
event.preventDefault()
event.stopPropagation()