使用“?”时滚动到 id 元素 在网址中,vue.js

art*_*jpm 3 javascript anchor-scroll vue.js

我有这个 url http://localhost:8080/?london并且它需要加载到 london in <section id="london">.- 这是页面上的id 。我不能使用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();
Run Code Online (Sandbox Code Playgroud)

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

类型错误:el.scrollIntoView 不是 VM10022 IE 工作中的函数:4

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

Son*_*Tr. 9

您尝试调用.scrollIntoView()字符串。

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


小智 9

对我来说效果很好

const el = document.getElementById('item');
      el.scrollIntoView({behavior: "smooth"});
Run Code Online (Sandbox Code Playgroud)