我在我的网页中使用openwysiwyg编辑器。我想清除它的内容。我用过
$('#report').val('');
但这并不清楚。
编辑器创建一个 iframe 并更新那里的内容,同步进行。
我将如何清除它?
我在我的网页中使用openwysiwyg编辑器。我想清除它的内容。我用过
$('#report').val('');
但这并不清楚。
编辑器创建一个 iframe 并更新那里的内容,同步进行。
我将如何清除它?
您可能需要提供更多信息 - html 本身将非常有用,但我将假设这report
是您需要清除的 textarea 的 id。
如果它是一个普通的文本区域,你的代码应该真的可以工作。
如果(正如 Paulo 在评论中提到的)它正在被 openwysiwyg 编辑器修改,它可能会变成一个 iFrame,里面有它自己的 HTML 页面。操作 iFrame 要困难得多。
看起来是这样的。
看看这个例子,看看它是否可以帮助您引用 iFrame 本身:http ://www.bennadel.com/index.cfm?dax=blog:1592.view
这是 openwysiwyg 附带的 example.html 的摘录:
<script type="text/javascript">
// Use it to attach the editor to all textareas with full featured setup
//WYSIWYG.attach('all', full);
// Use it to attach the editor directly to a defined textarea
WYSIWYG.attach('textarea1'); // default setup
WYSIWYG.attach('textarea2', full); // full featured setup
WYSIWYG.attach('textarea3', small); // small setup
// Use it to display an iframes instead of a textareas
//WYSIWYG.display('all', full);
function getIFrameDocument( id )
{
var iframe = document.getElementById(id);
if (iframe.contentDocument) {
// For NS6
return iframe.contentDocument;
} else if (iframe.contentWindow) {
// For IE5.5 and IE6
return iframe.contentWindow.document;
} else if (iframe.document) {
// For IE5
return iframe.document;
} else {
return null;
}
}
function clearcontents()
{
getIFrameDocument('wysiwygtextarea1').body.innerHTML = '';
}
</script>
然后在页面的某处,我有一个清除按钮(实际上是 div):
<div style="width:120px;height:20px;background:#ff0000;text-align:center;display:block;" onclick="clearcontents();">Clear!</div>
请注意,您的 textarea 的 id 以 . 为前缀wysiwyg
。这就是 iFrame 的名称。
我已经在 Firefox 中对此进行了测试,但目前没有其他任何内容。我在网上找到的获取 iFrame 的代码,希望它适用于其他浏览器:)
这可行,但很丑陋:
var frame = WYSIWYG.getEditor('--ENTER EDITOR NAME HERE--');
var doc = frame.contentWindow.document;
var $body = $('html',doc);
$body.html('');
替换--ENTER EDITOR NAME HERE--
为您调用时传递给编辑器的任何内容attach
。
我相信这行得通
$('#report').text('');