我正在尝试将页面移动到<div>
元素。
我已经尝试了下一段代码,但没有用:
document.getElementById("divFirst").style.visibility = 'visible';
document.getElementById("divFirst").style.display = 'block';
发布于 2011-02-16 02:08:05
您可以使用锚点来“聚焦”div。即:
<div id="myDiv"></div>
然后使用以下javascript:
// the next line is required to work around a bug in WebKit (Chrome / Safari)
location.href = "#";
location.href = "#myDiv";
发布于 2014-03-10 12:17:07
scrollIntoView可以很好地工作:
document.getElementById("divFirst").scrollIntoView();
MDN文档中的完整参考:
https://developer.mozilla.org/en-US/docs/Web/API/Element.scrollIntoView
发布于 2018-10-23 00:59:55
您可以将焦点设置为元素。它比scrollIntoView
工作得更好
node.setAttribute('tabindex', '-1')
node.focus()
node.removeAttribute('tabindex')
https://stackoverflow.com/questions/5007530
复制相似问题