contentEditable span - preventDefault无法正常工作

时间:2016-06-04 02:54:33

标签: javascript events

在编辑可编辑范围时,尝试停止创建。我试过搜索但没有运气。按键检测正常,通过警报测试,但只是preventDefault();似乎不起作用。我已经搜索过,这似乎是其他人阻止在进入时创建的方式。

$(document).on('focus', '.note>span', function(){ //edit notes
        var storedNote = $(this).text();
        var storedNoteIndex = notes.indexOf(storedNote);
        $(this).keyup(function (e) { //edit notes
            var newNote = $(this).text();
            if(newNote != "" && newNote != " " && newNote.charAt(0) != " ") {
                if (e.keyCode == 13 && e.shiftKey) {
                    alert('enter+shift');
                }
                else if(e.keyCode == 13){
                    e.preventDefault();
                }
                var newNote = $(this).text();
                notes.splice(storedNoteIndex, 1, newNote);
                chrome.storage.sync.set({'notes':notes});
            }
            else{
                notes.splice(storedNoteIndex, 1);
                chrome.storage.sync.set({'notes':notes});
            }
        });
    })

这是匹配js的html:

        <div id="icon-wrapper">
            <div id="notes-icon" class="icons glyphicon glyphicon-list-alt"></div>
            <div id="bg-left" class="icons glyphicon glyphicon-chevron-left"></div>
            <div id="bg-right" class="icons glyphicon glyphicon-chevron-right"></div>
            <div id="refresh" class="icons glyphicon glyphicon-refresh"></div>
            <div id="pin" class="icons"></div>
        </div>

        <div id="time-wrapper">
            <div id="time"></div>
            <div id="date"></div>
        </div>

        <div id="input-wrapper">
            <input type="text" id="input-box" placeholder="Create a note">
        </div>

        <div id="notes-wrapper">
            <div class="note">
               <span contenteditable="true"></span>
               <div class="remove glyphicon glyphicon-remove"></div>
            </div>
        </div>

1 个答案:

答案 0 :(得分:1)

我认为表单是在keydown上提交的,所以当您的keyup事件发生时,表单已经提交。尝试将其更改为

&#13;
&#13;
$(this).on('keydown keyup', function(e) {
  console.log(e);
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body contenteditable>Change Me</body>
&#13;
&#13;
&#13;

相关问题