2014-10-10 55 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