假设我有一个链接
<a href="http://URL"></a>
我想显示一个包含该链接内容的 IFRAME 来代替此链接。示例:
<iframe src="http://URL"></iframe>
此 iframe 的宽度将为 100%,并且 iframe 的高度将动态计算为等于结果页面的高度(也称为 http://URL )。
我如何使用纯 Javascript 解决方案来做到这一点?
最佳答案
您可以尝试此操作,但它仅适用于同一域名。这是安全限制
<html>
<body>
<div>
<a href="http://example.org" class="frame">example.org</a>
</div>
<script>
var links = document.getElementsByClassName('frame');
for (var i = 0; i < links.length; i++) {
var frame = document.createElement("iframe");
frame.setAttribute('src', links[i].getAttribute('href'));
frame.setAttribute('class', links[i].getAttribute('class'));
frame.style.width = "100%";
frame.onload = function() {
frame.style.height = frame.contentWindow.document.body.scrollHeight + "px";
};
links[i].parentNode.replaceChild(frame, links[i]);
}
</script>
</body>
</html>
关于javascript - 使用 Javascript 将链接转换为 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37064197/