0

有人可以帮我更新 htmleditor 中的 div 吗?它在 iframe 中,所以我不能这样做:

Ext.fly('mydiv').update('New value');

现在我正在更新整个内容:

xtype:'textfield',
listeners:{
                keyup:{
                    fn:function(){
                        var e = this.up().down('htmleditor');
                        var v = 'some text <div id='mydiv'></div> some more text'
                        e.setValue(v);
                    }
                }
            },
xtype:'htmleditor',
...

因为我只想更新 mydiv 部分,但无法以某种方式引用它。谢谢。

4

1 回答 1

0

如果我理解正确,这里真正的问题是“如何从其父文档访问 IFRAME 内的 DIV?”

我没有用过 EXT JS,所以我不知道它的语法。但这里是基本的 JavaScript。

一、IFRAME中的文件:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Child Document</title>
</head>
<body>

<h1>Child Document</h1>

<div id="stuff">
This is a div with some text in it.
</div>

</body>
</html>

然后是父文档:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Parent Document</title>
    <style type="text/css">
     #iframe {
        width: 250px;
        height: 250px;
        border: 1px solid #CCC;
    }
    </style>
</head>
<body>

<h1>Parent Docment</h1>

<form action="" method="get">
    <p>
    <input type="button" id="get-it" value="Get It" />
    </p>
</form>

<iframe src="iframe.html" id="iframe"> </iframe>

<script type="text/javascript">
<!--
function getIt(){
    var iframe = document.getElementById("iframe");
    var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
    var stuff = innerDoc.getElementById("stuff");
    alert(stuff.innerHTML);
}

function init(){
    document.getElementById("get-it").onclick = getIt;
}

window.onload = init;

// -->
</script>

</body>
</html>

关键是这样的:

var innerDoc = iframe.contentDocument || iframe.contentWindow.document;

这使您可以访问 IFRAME 的文档对象,之后您可以正常使用它。

于 2011-05-20T17:27:40.830 回答