52

「window.location.hash」を新しく学び、「window.location.href の代わりにjqueryコードで試してみましたが、どちらも同じ結果になりました。

コードはここにあります:

window.location.href = ($(e.currentTarget).attr("href"));
window.location.hash = ($(e.currentTarget).attr("href"));

それらの違いは何ですか?

4

6 に答える 6

74

のような URL の場合http://[www.example.com]:80/search?q=devmo#test

hashは、# 記号を含む、# 記号に続く URL の部分を返します。hashchange イベントをリッスンして、サポートしているブラウザーでのハッシュへの変更の通知を受け取ることができます。

Returns: #test

hrefは URL 全体を返します。

Returns: http://[www.example.com]:80/search?q=devmo#test

続きを読む

于 2012-05-21T15:49:10.873 に答える
11

たとえば、それをテストしますhttp://stackoverflow.com/#Page

href = http://stackoverflow.com/#Page
hash = #Page
于 2012-05-21T15:49:09.187 に答える
4

href は URL です

ハッシュは URL の後のアンカーのみです

http://www.xxxxxxx.com#anchor

http://www.xxxxxxx.com#anchorは href です

「#anchor」はハッシュ

于 2012-05-21T15:47:38.987 に答える
3

hashhrefはどちらもwindow.locationオブジェクトのプロパティです。hashは URL の一部#(または がない場合は空の文字列#) であり、hrefは URL 全体の文字列表現です。

于 2012-05-21T15:48:17.677 に答える
2

hash プロパティは、ハッシュ記号 (#) を含む URL のアンカー部分を返します。

于 2012-05-21T15:48:24.753 に答える