我有一个包含iframe的html页面:

<html>
<body>
<iframe id = "i1" name = "i1" src = "C:\Documents and Settings\adivekar\Desktop\sa.html"></iframe>
<br><input type = "text" name = "t1" id = "One"/>
<br><input type = "button" value = "change1" name = "b1" onclick = "call1()">
<script type = "text/javascript">
function call1(){
    var a = document.getElementById('i1');
    var b = a.contentDocument;
    b.open()
    var c = b.getElementById('Two');
    c.value = "dfsdf";
}
</script>
</body>




包含iframe的页面是:

<html>
<body>
<input type = "text" name = "t2" id = "Two"/>

</body>
</html>


我无法更新iframe中文本框的值。

最佳答案

如果iframe与主文档不在同一个域中,则您将无法访问iframe的文档。如果是这样,您的代码应可在大多数浏览器中使用。对于不支持ifame元素的contentDocument属性的浏览器,我将对其稍作更改,并删除对iframe文档的open()方法的调用,这是不必要的,并且可能会引起问题:

function call1(){
    var a = document.getElementById('i1');
    var b = a.contentDocument || a.contentWindow.document;
    var c = b.getElementById('Two');
    c.value = "dfsdf";
}

10-07 16:15
查看更多