我正在为一个应用程序创建一个简单的RTE,它不需要太复杂,所以我认为使用execCommands可能是最好的解决方案。然而,我遇到了麻烦,因为命令创建了无效的HTML。这是我正在使用的javascript,如果能深入了解是什么导致了这个问题,我们将不胜感激。
$('.turnEditOn').live('click',function(){
richTextEditor.document.designMode = 'ON';
$('#richTextEditor').focus(); });
$('#bold').live('click',function(){
richTextEditor.document.execCommand('bold',false,null);
$('#richTextEditor').focus(); });
$('#underline').live('click',function(){
richTextEditor.document.execCommand('underline',false,null);
$('#richTextEditor').focus(); });
$('#italic').live('click',function(){
richTextEditor.document.execCommand('italic',false,null);
$('#richTextEditor').focus(); });发布于 2012-05-04 14:19:30
HTML:
<div id="turnEditOn" class="command">on</div>
<div id="bold" class="command"><b>B</b></div>
<div id="italic" class="command"><i>I</i></div>
<div id="underline" class="command"><u>U</u></div>
<div class="command" onclick="alert(document.getElementById('richTextEditor').contentDocument.body.innerHTML);">Show HTML</div>
<br/>
<iframe id="richTextEditor" style="width:500px;height:300px;"></iframe>JAVASCRIPT+jQuery(onDomReady):
var richTextEditor=document.getElementById("richTextEditor");
$('#turnEditOn').live('click',function(){
richTextEditor.contentDocument.designMode = 'ON';
richTextEditor.contentDocument.body.contentEditable=true;
// Switch FireFox RTE execCommand engine to work in same manner as IE
try{richTextEditor.contentDocument.execCommand('styleWithCSS',false,false);}
catch(e){}
richTextEditor.focus();
});
$('#bold').live('click',function(){
richTextEditor.contentDocument.execCommand('bold',false,null);
richTextEditor.focus();
});
$('#underline').live('click',function(){
richTextEditor.contentDocument.execCommand('underline',false,null);
richTextEditor.focus();
});
$('#italic').live('click',function(){
richTextEditor.contentDocument.execCommand('italic',false,null);
richTextEditor.focus();
});示例:http://jsfiddle.net/d2L2A/
阅读有关Rich Text Editing in MDN的信息
https://stackoverflow.com/questions/10442908
复制相似问题