3

我有这个 url http://localhost:8080/?london并且它需要加载到 london 的 id <section id="london">.- 这是在页面上。我不能使用http://localhost:8080/#london,即使这样可以!

为了让事情变得更复杂,我使用了一个 vue 框架,他们想要 master-origin/src/assets/js/mixins/Anchor.js 中的代码 - 因此我无法安装 jquery,必须是 vanilla js。

我努力了

var el = [];
el = window.location.href.split("/?")[1];
console.log("el: " + el);
el.scrollIntoView();

哪个在/?之后获取值,但是当我尝试scrollIntoView时,我在控制台中收到此消息

TypeError: el.scrollIntoView is not a function at VM10022 IE work:4

为什么?我正在关注 w3 学校指南https://www.w3schools.com/jsref/met_element_scrollintoview.asp

4

2 回答 2

12

你尝试调用.scrollIntoView()一个字符串。

尝试这个:document.getElementById(el).scrollIntoView();

于 2018-11-17T14:35:39.370 回答
5

对我有用

const el = document.getElementById('item');
      el.scrollIntoView({behavior: "smooth"});
于 2020-08-16T19:13:39.173 回答