window.location =#锚在IE中不起作用 [英] window.location = #anchor doesn't work in IE
问题描述
在此图上:
http://web.pacific.edu/documents/marketing/campus-map/version%202/stockton-campus-2.0.htm
我有一个锚点在顶部,我希望页面在点击链接时跳转到锚点。
我正在使用
window.location ='#top';
在FF,Opera和Chrome中可以正常使用,但不会在IE 7中使用。
我尝试了所有像window.location.hash和window.location.assign()以及scrollIntoView(true)和focus()的排列组合。
如何让它在IE中工作?
编辑:似乎没有工作,这让我觉得这不是语法,但有关JS的东西......这里是点击事件处理程序...可能是因为它返回false?我正在抓秸秆。
//点击每个位置的链接处理链接
$('#index a')。click(function()
{
hideMarkers();
location.href = location.href +#top;
var marker = showMarker($(this).attr('data-id '));
GEvent.trigger(marker,click);
return false;
});
编辑:分配给IE7和IE8中的window.location.hash分隔符在通过HTTP位置标头进行页面重定向时加载的页面上。解决方案是返回一个页面,该页面本身将执行重定向。请参阅Joe Lapp的答案。
我在生产中使用此代码,并且在IE7中正常工作...
location.hash =#top;
然而,如果您只是想滚动到顶部,这应该会容易得多。 ..
window.scrollTo(0,0);
On this map:
http://web.pacific.edu/documents/marketing/campus-map/version%202/stockton-campus-2.0.htm
I have an anchor at the top, and I want the page to jump to the anchor when a link is clicked.
I'm currently using
window.location = '#top';
It works as expected in FF, Opera, and Chrome, but not in IE 7.
I've tried all permutations like window.location.hash and window.location.assign() and also scrollIntoView(true) and focus().
How can I make it work in IE?
Edit: Nothing seems to work, which makes me think it's not the syntax, but something about the JS... here is the click event handler... could it be because it returns false? I'm grasping at straws.
// Click handler for each location link
$('#index a').click(function()
{
hideMarkers();
location.href = location.href + "#top";
var marker = showMarker( $(this).attr('data-id') );
GEvent.trigger( marker, "click" );
return false;
});
Edit: Assignment to window.location.hash breaks in IE7 and IE8 on pages that were loaded as a result of page redirection via the HTTP "Location" header. The solution is to return a page with Javascript that itself will perform the redirection. See the answer by Joe Lapp.
I have this code in production and it works fine in IE7...
location.hash = "#top";
However, if you are just trying to scroll to the top, this ought to be a lot easier...
window.scrollTo(0, 0);
这篇关于window.location =#锚在IE中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!