我有一个HTML字符串如何设置HTML内容到iframe
<html>
<body>Hello world</body>
</html>
,我想将其设置为包含JavaScript的iframe中。我想这样设置HTML:
contentWindow.document.body.innerHTML
或
contentDocument.body.innerHTML
或
document.body.innerHTML
但IE给出了 “访问被拒绝”。或“对象不支持此属性或方法”。或“操作的最终元素无效”。错误。
这里是我的全码:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery_1.7.0.min.js"/>
<script type="text/javascript">
$(document).ready(function(){
var htmlString = "<html><body>Hello world</body></html>";
var myIFrame = document.getElementById('iframe1');
// open needed line commentary
//myIFrame.contentWindow.document.body.innerHTML = htmlString;
//myIFrame.contentDocument.body.innerHTML = htmlString;
//myIFrame.document.body.innerHTML = htmlString;
//myIFrame.contentWindow.document.documentElement.innerHTML = htmlString;
});
</script>
</head>
<body>
<p>This is iframe:
<br>
<iframe id="iframe1">
<p>Your browser does not support iframes.</p>
</iframe>
</body>
</html>
为什么不加载一个页面,它工作得很好?为什么你想使用一个字符串来加载?让我知道 – 2013-05-15 05:13:12