使用“?"时滚动到 id 元素;在网址中,vue.js [英] scroll to id element when using "?" in url, vue.js

查看:28
本文介绍了使用“?"时滚动到 id 元素;在网址中,vue.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 url http://localhost:8080/?london 并且它需要加载到<section id="london">. 中的伦敦 id - 位于页面上.我不能使用 http://localhost:8080/#london,即使它会起作用!

I have this url http://localhost:8080/?london and it needs to load to the id of london in <section id="london">.- which is on the page. I cannot use http://localhost:8080/#london, even though that will work!

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

To make things more complicated I am using a vue framework and they want the code inside master-origin/src/assets/js/mixins/Anchor.js - therefore I can't install jquery, has to be vanilla js.

我试过了

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

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

Which grabs the value after the /?, but I when I tried to scrollIntoView, I get this message in the console

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

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

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

Why? I am following w3 schools guide https://www.w3schools.com/jsref/met_element_scrollintoview.asp

推荐答案

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

You try to call .scrollIntoView() on a string.

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

这篇关于使用“?"时滚动到 id 元素;在网址中,vue.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆