2013-10-26 17 views
1

我試圖用ajax重新加載頁面。網址中包含哈希(ANCOR)window.location.reload不考慮散列在ie

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

我試圖用location.reload()或windows.location.reload(真)

$(".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); 
} 

}); 
+0

你有一個URL你可以分享,所以我可以試試嗎? –

回答

2

你可以嘗試使用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); 
    } 
}); 
+0

不幸的是不起作用。甚至沒有重新加載.. –

+0

@PaoloRossi我的代碼不會重新加載頁面,因爲它不是代碼的替代品。要重新加載,請使用您當前的代碼 – Oriol

+0

我嘗試在我的重新加載點擊功能後添加您的代碼,但出現錯誤。怎麼了?請看看我的編輯。 –