编辑器的pasteHTML方法,如果插入标签字符串例如pasteHTML("<span>内容</span>"),span会转义成标签,只显示"内容",但我想要的效果是显示"<span>内容</span>",即span标签只是字符串,有什么解决方法?<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link rel="stylesheet" type="text/css" href="css/stylel.css"/>
<script>
var getIframeByClass=function(classname){
var ifms=document.getElementsByTagName("iframe");
for(var i=0;i<ifms.length;i++){
if(ifms[i].className.indexOf(classname)>-1){
return ifms[i];
}
}
return null;
}
var extendJson=function(destination,source){
for(var json in source){
destination[json] = source[json];
}
return destination;
}
var teditor=function(options){
this._init(options);
this._exec();
}
teditor.prototype={
_init:function(options){
this.ieRange=false;
this.options={
editor:getIframeByClass("tEditor")
};
this.options = extendJson(this.options,options);
},
_exec:function(){
this.win=this.options.editor.contentWindow;
this.doc=this.win.document;
//txt=document.getElementById('txt');    
this.doc.designMode='On';//可编辑
this.win.focus();
},
_saveRange:function(){//IE下保存Range对象
if(!!document.all&&!this.ieRange){//是否IE并且判断是否保存过Range对象
var sel=this.doc.selection;
this.ieRange=sel.createRange();
if(sel.type!='Control'){//选择的不是对象
var p=this.ieRange.parentElement();//判断是否在编辑器内
if(p.tagName=="INPUT"||p==document.body)this.ieRange=false;
}
}
},
_insert:function(text){
this._saveRange();
if(this.ieRange){
this.ieRange.pasteHTML(text);
this.ieRange.select();
this.ieRange=false;//清空下range对象
}else{//焦点不在html编辑器内容时
this.win.focus();
if(document.all){
this.doc.body.innerHTML+=text;//IE插入在最后
}else{//Firefox
var sel=this.win.getSelection(),rng=sel.getRangeAt(0),frg=rng.createContextualFragment(text);
rng.insertNode(frg);
}
}
}
}
window.onload=function(){
var editor=new teditor({editor:document.getElementById("tagEditor")});
document.getElementById("tagid").onmousedown=function(){editor._insert(this.title)};
};
</script>
</head><body>
<table width="600" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<div class="tagsBox">
<a href="#" id="tagid" title="<span>内容</span>">id</a>
<a href="#" title="name" onclick='alert(document.getElementById("tagEditor").contentWindow.document.body.innerHTML)'>name</a>
<a href="#" title="time">time</a>
</div>
</td>
</tr>
<tr>
<td><iframe width="100%" id="tagEditor" name="tagEditor" class="tEditor"></iframe></td>
</tr>
</table></body>
</html>