关于javascript:如何在iframe`srcdoc`中使href锚实际起作用

How to make href anchors in iframe `srcdoc` actually work

我在iframe中加载了一些html内容。 我正在通过srcdoc属性加载html内容。 不幸的是,当跟随iframe中的定位标记链接时,它会将整个页面加载到iframe中,而不是滚动到带有链接ID的标签。

演示问题的JSFiddle:https://jsfiddle.net/kzLdqjgs/5/

我可以使用src="data:text/html,

my html

"使锚标记起作用,但是我很好奇为什么它与srcdoc不兼容。 此外,某些实现的src属性显然有32,768个字符的限制,而我尝试加载的内容的长度超过了60k个字符(尽管在Chrome中似乎可以正常工作)。


我发现iframe srcdoc中的href="about:srcdoc#some_id可以工作(至少在Chrome中,我没有在其他浏览器中对其进行测试)。

从维基百科:

about:srcdoc: A reserved, though unresolvable, URI defined within HTML5 intended to be the URI navigated to within iframes whose content comes from the srcdoc attribute.

在这里查看链接