JQuery.address如何从url中删除#(javascript window.location删除#)
当我的 event.value == 到 / 时,我需要从 url 中删除 #。 我有一个带有 jquery.address 的 lightbox,它存储对打开图像的引用,当我关闭它时,我需要删除 # 标记,因为这会导致窗口滚动到顶部。
我成功删除了 # 标记: window.location.href.slice(0, -1);但正如您在上面的代码中看到的,这会导致在加载页面时进行 url 重写,而不仅仅是在我的活动之后。
仅当我完成时,我如何才能链接此 JavaScript,就像当我关闭灯箱时此函数被称为 olny 一样。
我在这里附上我的代码和注释。 谢谢大家
$.address.change(function(event) {
curLink = event.value;
if(curLink != '/') {
// here my stuff, jquery.address generates url with reference
// ex: mysite.com/cat/subcat/page.html#one
} else {
$('#element').animate({opacity:"0"},{duration:100, easing:"quartEaseOut", complete: function () {
// here I need to remove the hash only after the complete
// ex: mysite.com/cat/subcat/page.html# > mysite.com/cat/subcat/page.html
window.location.href.slice(0, -1);
$(this).hide();
}});
}
});
I need to remove the # from url when my event.value is == to /.
I've got a lighbox with jquery.address that stores references to open images, when i close it i need to remove the # mark becouse this cause window scroll top.
I succed remove the # mark with this: window.location.href.slice(0, -1); but as you can see in the code above this cause the url rewrite when page is loaded and not only after my event.
How can i chain this javascript only when my complete happens, in the way this function is called olny when i close the lightbox.
I attach here my code with comments.
thank you all
$.address.change(function(event) {
curLink = event.value;
if(curLink != '/') {
// here my stuff, jquery.address generates url with reference
// ex: mysite.com/cat/subcat/page.html#one
} else {
$('#element').animate({opacity:"0"},{duration:100, easing:"quartEaseOut", complete: function () {
// here I need to remove the hash only after the complete
// ex: mysite.com/cat/subcat/page.html# > mysite.com/cat/subcat/page.html
window.location.href.slice(0, -1);
$(this).hide();
}});
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你也可以使用这个(它完全删除哈希值并清理 URL),
它会更改
为
You could also use this (which totally removes the hash and cleans the URL right back up)
Which changes
to
使用
window.location.hash = ''
应该彻底删除它,否则有一些很好的插件可以打开很多用于处理 URL 的功能(例如 http://ajaxcssblog.com/jquery/url-read-request-variables)。Using
window.location.hash = ''
should remove it outright, otherwise there are some good plugins that open up a lot of functions for working with URLs (e.g. http://ajaxcssblog.com/jquery/url-read-request-variables).如果您有锚点,请尝试此操作
,那么它通常会附加在 URL 的开头,因此删除第一个字符后,您就不应该再将其视为问题了。
或者如果你甚至可以这样做,我认为这就是你可能需要的
try this
if you have an anchor then its usually appended at the beginning of the URL so but removing the first character then, you shouldn't have it as an issue anymore.
or if you can even do it this way which i think is what you might need
我发现在哈希之后更改值最适合与您类似的场景。它必须是您的页面上尚不存在的哈希值。
I've found that changing the value after the hash worked best for a scenario similar to yours. It would need to be a hash that doesn't already exist on your page.
window.location.hash = ''
在我的情况下不起作用。window.location.href.slice(0, -1);
有效!我无法解决的问题是如何仅在完成 jQuery 事件之后才链接此 JavaScript 指令(现在它是在页面加载时触发的)。我认为现在在 jQuery 之后调用 JavaScript 是愚蠢的,
谢谢
window.location.hash = ''
don't work in my case.window.location.href.slice(0, -1);
works! the problem i can't solve is how I can chain this JavaScript instruction only after the complete jQuery event (by now it is fired on page load).I think by now is stupid JavaScript call after jQuery
thank you