2014-10-10 74 views
0

我已經在config中打開allowedContent屬性。CKeditor內聯:重複段落ID

config.allowedContent = "true" 

這使我可以在contenteditable div中的段落添加id。

但是,現在無論何時我在contenteditable div中敲入enter鍵,都會生成一個具有相同id的新段落。我會假設在hiiting之後輸入一個新的段落時應該插入沒有任何id的,但它看起來像是從以前生成的段落複製id。

有什麼辦法可以避免這種情況?

+0

您可以使用['afterCommandExec'](http://docs.ckeditor.com/ #!/ api/CKEDITOR.editor-event-afterCommandExec)在[this plugin](https://gist.github.com/Reinmar/1acce88add99964be1c6)中,並從當前(新)段落中刪除'id'。 – Reinmar 2014-10-10 07:36:57

+0

謝謝,解決了我的問題 – Coder 2014-10-13 02:54:25

+0

@Reinmar添加爲答案? @mundella - 使用'true'不''「true」'。在布爾中,不是字符串。 – Nenotlep 2014-10-13 06:58:38

回答

0

試試這個。這不是防彈的,但工作得很好。雖然我寫的,我有點恨它,所以如果你改進它,請分享愛;)

editor.on('key', function (evt) { 
    // Only if editor is not in source mode. 
    if (editor.mode === 'source') { return; } 

    // Enter is keyCode 13 
    if (evt.data.keyCode === 13) { 
     // if we call getStartElement too soon, we get the wrong element sometimes 
     setTimeout(function() { 
      var selection = editor.getSelection(); 

      if (typeof selection === 'undefined') { return; } 

      var startElement = selection.getStartElement(); 

      // If there are spans nested in the paragraph preserve them 
      // And we need to find the parent paragraph 
      // This could be optimized... 
      if (startElement.getName() == 'span') { 
       var text = ""; 
       while (startElement.getName() == 'span') { 
        text += startElement.getHtml(); 
        startElement = startElement.getParent(); 
       } 
       if (text.length === 0) { 
        startElement.setHtml(' '); 
       } else { 
        startElement.setHtml(text); 
       } 
      } 

      // HERE I remove the "id" attribute. 
      startElement.removeAttribute("id");; 
     }, 10); 
    } 
}); 
+0

我知道這已經解決了,我只是想分享我的方法來獲得一些cricisim/comments/points並給出一個替代方案。 @mundella如果你也添加你的解決方案,並接受它,以便其他人可以從中受益,那麼它會很搖滾。特別是如果它比我的蹩腳方法更好:D – Nenotlep 2014-10-17 06:59:41