「document.location.hash」でURLを削除し、現在のURLから何が残っているかを確認しようとしています。プラグインを使用したくないし、クリックなどのトリガー イベントから結果を学習したくありません。URL の変更を即時かつ現代的に学習する必要があります。
jQuery:
$(function() {
$(document).location().change(function() {//this part is not working
var anchor = document.location.hash;
if( anchor === '#one' ) {
alert('current url = #one');
}else if ( anchor === '#two' ) {
alert('current url = #two');
}else if ( anchor === '#three' ) {
alert('current url = #three');
}
console.log(anchor);
});
});
html:
<a href="#one">one</a>
<a href="#two">two</a>
<a href="#three">three</a>
深い注意: 私はこれらの質問を読み、私が探しているものを見つけました: JavaScript で URL の変更を検出する方法+ JavaScriptで現在の URL を変更する方法?