87,910
社区成员
发帖
与我相关
我的任务
分享
{//HtmlEditor target 预替换的 textarea 节点id
function HtmlEditor(target) {
if (!(target = document.getElementById(target))) return false;
var iframe = document.createElement('iframe');
this.textarea = target;
this.iframe = iframe;
var iframeTextContent = ''
iframeTextContent += '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
iframeTextContent += '<html xmlns="http://www.w3.org/1999/xhtml" >';
iframeTextContent += '<head> <title>text edit area</title>';
iframeTextContent += '<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />';
iframeTextContent += '<link rel="stylesheet" type="text/css" href="../css/mxjia_config.css" /></head>';
iframeTextContent += '<body style="padding:3px;margin:0px;font-size:12px;cursor:text;text-align:left;line-height:16px;">测试</body></html>';
this.iframeTextContent = iframeTextContent;
}
HtmlEditor.prototype = {
//获取TextArea对象
getTextArea: function() {
return this.textarea;
},
//获取iframe对象
getIframe: function() {
return this.iframe;
},
//获取iframe引用的子窗体对象
getWin: function() {
return this.iframe.contentWindow;
},
//获取iframe引用窗体对象的document对象
getDoc: function() {
return this.getWin().document;
},
//获取iframe应用窗体的body对象
getBox: function() {
return this.getDoc().body;
},
//开启document对象的 DesignModel
openDocDesignMode: function() {
try {
this.getDoc().open();
this.getDoc().write(this.iframeTextContent);
this.getDoc().close();
this.getDoc().designMode = 'on';
}
catch (e) {
setTimeout(arguments.callee, 10);
}
},
//创建并初始化iframe对象
init: function() {
this.iframe.id = 'iframe_' + this.textarea.id || 'iframe_htmlEditor';
this.iframe.className = this.textarea.className;
mxjia.insertAfter(this.iframe, this.textarea);
this.textarea.style.display = 'none';
this.openDocDesignMode();
alert(this.getDoc().body); //这里 在ie中 打印出 null 其他浏览器全部正常....
}
}
}