更改iframe的内容

时间:2011-02-07 11:45:49

标签: javascript html

我有一个由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中文本框的值。

2 个答案:

答案 0 :(得分:2)

如果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";  
}

答案 1 :(得分:0)

尝试

document.getElementById('i1').getElementById('Two').value = "..";