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

时间:2018-11-17 14:25:52

标签: javascript vue.js anchor-scroll

我有这个URL http://localhost:8080/?london,它需要加载到页面上<section id="london">.中的伦敦ID。 即使可以使用,我也无法使用http://localhost:8080/#london

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

我尝试过

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

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

  

TypeError:el.scrollIntoView不是函数       在VM10022 IE工作:4

为什么?我正在追踪W3学校指南https://www.w3schools.com/jsref/met_element_scrollintoview.asp

2 个答案:

答案 0 :(得分:2)

对我来说很棒

const el = document.getElementById('item');
      el.scrollIntoView({behavior: "smooth"});

答案 1 :(得分:1)

您尝试在字符串上调用(-> "apple" (first) ; Is the first letter #{\a \e \i \o \u}) ; In the set of vowels? => \a ; Truthy! (-> "hello" (first) #{\a \e \i \o \u}) => nil

尝试以下操作:.scrollIntoView()