2010-04-26 138 views
3

我正在尝试实现项目,并且我必须在JavaScript中执行WYSIWYG编辑器。我无法使用现有的编辑器,因为我需要使用我的插件(例如colorPickerimagePicker)。getSelection on DIV contentEditable

现在我有这样的HTML:

<div class="K_editor" id="idExample"> 
    <div class="K_links"> 
     <div class="K_editor_link K_editor_linkBold">B</div> 
     <div class="K_editor_link K_editor_linkItalic">I</div> 
     <div class="K_editor_link K_editor_linkUnderline">U</div> 
    </div> 
    <iframe width="696" height="212" frameborder="0" src="js/myEditor_iFrame.php"> 
     <html> 
     <head/> 
     <body> 
      <div id="contentIframe" contenteditable="true"> 
       This is a test code, with <strong>bold</strong> text and <em>italic</em> text. 
      </div> 
     </body> 
     </html> 
    </iframe> 
    <input type="submit"/> 
</div> 

.K_editor_link事件点击,一个功能与参数开:

  • tagStart(例如<u>,或<span style="color:#AB1;">
  • tagEnd(示例</u></span>
  • id(这里idExample

我知道得到TextareasetSelectionRange().selectionStart.selectionEnd一个选择是只对textbox(XUL),input(XHTML)或textarea(XHTML)。

我能做些什么呢?

+0

,你必须使用自己的插件真的不强迫你写自己一个WYISWYG编辑器的事实。 CKEditor和TinyMCE允许使用自己的插件,实际上它们是围绕一个核心构建的一组插件。 – AlfonsoML 2011-12-10 18:27:28

回答

0

这是我使用的代码。自从几个月前我在这里发现它之后,我无法赞扬它。不记得在哪里。希望可以帮助你。

function getSelectionHtml() 
{ 
    var html = ""; 

    if (typeof window.getSelection != "undefined") 
     { 
     var sel = window.getSelection(); 
     if (sel.rangeCount) 
      { 
      var container = document.createElement("div"); 
      for (var i = 0, len = sel.rangeCount; i < len; ++i) { 
       container.appendChild(sel.getRangeAt(i).cloneContents()); 
      } 
      html = container.innerHTML; 
     } 
    } else if (typeof document.selection != "undefined") { 
     if (document.selection.type == "Text") { 
      html = document.selection.createRange().htmlText; 
     } 
    } 
    return html; 

} 

代码是从这样一个问题:window.getSelection return html