admin管理员组文章数量:1355675
I have documents with links to internal anchors within a document. That is to say, I have a document that has a link in the form <a href="toc">Table of Contents</a>
that when clicked will scroll to the anchor <a id="toc">Table of Contents</a>
.
However if I put this document in an iframe using srcdoc, adding <base href="about:srcdoc" />
to the head of my document to work around the issues How to make href anchors in iframe `srcdoc` actually work describes, I can see window.location.hash
in my iframe update correctly, but the page does not actually scroll. I've tried this using Windows 64bit Firefox 136.0.4 and Chrome 134.0.6998.178.
To see this for yourself:
<html>
<head></head>
<body>
<div id="div1" style="height: 4000px">
First div
<a href="#div2">Go to second div</a>
</div>
<div id="div2" style="height: 4000px">
Second div
<a href="#div1">Go to first div</a>
</div>
</body>
</html>
Is a simple test document. If you save it as a .html file, load it in a browser, and click the links, you'll see the page scroll.
<html>
<head></head>
<body>
<iframe srcdoc="<html>
<head>
<base href="about:srcdoc" />
</head>
<body>
<div id="div1" style="height: 4000px">
First div
<a href="#div2">Go to second div</a>
</div>
<div id="div2" style="height: 4000px">
Second div
<a href="#div1">Go to first div</a>
</div>
</body>
</html>" style="width: 100%; height:10000px"></iframe>
</body>
</html>
Is a page with that simple test document in an iframe using a srcdoc, with the base URL set to make anchors work as described above. If you save it as a .html file, load it in a browser, and click the links, the page will not scroll.
If you open a Developer console, navigate so you're running Javascript in the context of the iframe, and run window.location.hash
, you'll see that the hash is being updated correctly. It's just that the page is not scrolling in response to that.
本文标签: htmlLinks to anchors within a document do not scroll the page with iframe using srcdocStack Overflow
版权声明:本文标题:html - Links to anchors within a document do not scroll the page with iframe using srcdoc - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743970282a2570626.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论