获取URL中的锚

March 16, 2021

需求:跨页面跳转到锚点定位,并给锚点元素添加点击事件,用于跳转后自动展开手风琴。

实现:location.hash获取URL中锚部分,然后模拟一个点击事件。

var urlHash = window.location.hash;
if (urlHash.length > 0) {
  document.getElementById(urlHash).click();
}

Category

JavaScript

You may also like

Countdown to this year

No comments yet, leave a comment

Sending failed, please try again.
Sending succeeded! It will be displayed in one minute, please refresh.
Newer Older