编程语言
首页 > 编程语言> > javascript – window.location = #anchor在IE中不起作用

javascript – window.location = #anchor在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的东西…这里是click事件处理程序……可能是因为它返回false?我正在抓稻草.

// 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;
});

编辑:对于通过HTTP“Location”标题页面重定向加载的页面上的IE7和IE8中的window.location.hash赋值.解决方案是返回一个带有Javascript的页面,它本身将执行重定向.请参阅Joe Lapp的答案.

解决方法:

我在生产中有这个代码,它在IE7中运行良好…

location.hash = "#top";

但是,如果你只是想滚动到顶部,这应该更容易……

window.scrollTo(0, 0);

标签:javascript,internet-explorer,hash,anchor,window-location
来源: https://codeday.me/bug/20190713/1445471.html