window.location.reload不考虑在ie中的哈希

时间:2013-10-26 16:44:35

标签: internet-explorer jquery

我尝试使用ajax重新加载页面。网址包含哈希(ancor)

index.php?page=2&obj=3#lb

我尝试使用location.reload()或windows.location.reload(true)

$(".reload").click(function(){

    var userid = $(this).attr('userid');

    $.post("testpost.php", {userid:userid}, function(data){

    //window.location.reload(true);
            location.reload();

    });
});

使用FF,Chrome和Opera可以很好地工作,但是当重新加载页面时使用IE(即使在浏览器的URL中有哈希),也不会考虑ancor并且从上方查看页面。我怎么能解决这个问题?感谢

修改

$(document).ready(function() {

  $(".reload").click(function(){

  var userid = $(this).attr('userid');

  $.post("testpost.php", {userid:userid}, function(data){

    location.reload();

    });
  });

 var hash, el;
 if(hash = location.hash.substring(1) && el = document.getElementById(hash)) {
    el.scrollIntoView(true);
 }

});

1 个答案:

答案 0 :(得分:3)

您可以尝试使用location.hash.substring(1)获取元素的ID,使用document.getElementById获取,然后使用scrollIntoView

document.getElementById(location.hash.substring(1)).scrollIntoView(true);

在你的情况下,你希望它在重新加载后发生,所以你可以使用

$(document).ready(function() {
    var hash, el;
    if((hash = location.hash.substring(1)) && (el = document.getElementById(hash))) {
        el.scrollIntoView(true);
    }
});