location.hash的用法
location.hash属性用于获得或设置URL中的锚点部份(即#号后面的部份)。锚点部份通经常使用于定位到页面的特定部份或履行特定的行动。
用法示例:
1. 获得当前URL中的锚点部份:
```
console.log(location.hash);
```
如果URL为`http://example.com/page#section1`,则输出`#section1`。
2. 设置URL中的锚点部份:
```
location.hash = 'section2';
```
将当前URL中的锚点部份设置为`#section2`。
3. 监听URL中锚点部份的变化:
```
window.addEventListener('hashchange', function() {
console.log(location.hash);
});
```
当URL的锚点部份产生变化时,将会触发回调函数,并输出新的锚点部份。
TOP