如何防止在tinyMCE编辑器中删除div

时间:2015-04-07 12:13:49

标签: javascript html tinymce tinymce-4

当用户在编辑器上按下删除键或退格键时,我想阻止删除div .panel-heading和.panel-body。这是编辑器上出现的div:

<div class="panel panel-default tab">
    <div class="panel-heading "><h3 class="panel-title">test title</h3></div>
    <div class="panel-body "><p>test body</p></div>
</div>

1 个答案:

答案 0 :(得分:2)

在编辑器的设置功能上试试这个:

          ed.on("keydown",function(e) {
                //prevent empty panels
                if (e.keyCode == 8 || e.keyCode == 46) { //backspace and delete keycodes
                    try {
                        var elem = ed.selection.getNode().parentNode; //current caret node
                        if (elem.classList.contains("panel-body") || elem.classList.contains("panel-heading")) {
                            if (elem.textContent.length == 0) {
                                e.preventDefault();
                                return false;
                            }
                        }
                    } catch (e) {}
                }
            });