ハッシュを更新してからページをリロードしようとしています。
$('a[name="' + fragment + '"]').remove(); //don't jump before window reloads
window.location.hash = fragment;
window.location.reload(true);
ウィンドウをリロードした後、アンカー タグにジャンプしません。どうすれば修正できますか?
ハッシュを更新してからページをリロードしようとしています。
$('a[name="' + fragment + '"]').remove(); //don't jump before window reloads
window.location.hash = fragment;
window.location.reload(true);
ウィンドウをリロードした後、アンカー タグにジャンプしません。どうすれば修正できますか?
ページをリロードしている場合、これは jQuery で達成するのはかなり簡単です。window.location.hash
ページをロードするときにプロパティを確認してください。
$(document).ready( function( ) {
if( window.location.hash ) { // just in case there is no hash
$(document.body).animate({
'scrollTop': $( window.location.hash ).offset().top
}, 2000);
}
});
唯一の注意点は、ハッシュがスクロール先の要素の id と一致することです。
デモはこちら
MOZILLA DEVELOPER NETWORKは次の使用を提案しreplace
ます:
function reloadPageWithHash() {
var initialPage = window.location.pathname;
window.location.replace('http://example.com/#' + initialPage);
}