Detect keyup event of contenteditable child whose parent is a contenteditable div

前端 未结 1 792
孤城傲影
孤城傲影 2020-12-06 10:47

Goal: Attach a keydown event handler to a contenteditable span that is the child of a contenteditable div.

Problem: If you type in

相关标签:
1条回答
  • 2020-12-06 11:36

    So this is a bug. Workaround confirmed for FF23 and CHROME29 (on linux with no vm so can't test IE). You must set the wrapping spans as contenteditable false, you can't just omit declaring the contenteditable attribute, which is rediculous. Solution via Keypress event on nested content editable (jQuery)

    Here is the fiddle: http://jsfiddle.net/aBYpt/14/

    HTML

    <div class="parent" contenteditable="true">
        Parent div text.
    
        <span contenteditable="false">
            <span class="child" contenteditable="true">First child span text.</span>
        <span contenteditable="false">
            <span class="child" contenteditable="true">Second child span text.</span>
        </span>
    </div>
    
    <div id="console"></div>
    

    JavaScript/jQuery

    $(document).on( 'keyup', '.parent', function() {
        //do stuff
        $('#console').html( 'Parent keyup event fired.' );
    });
    
    $(document).on( 'keyup', '.child', function(e) {
        //do stuff
        $('#console').html( 'Child keyup event fired.' );
        e.stopPropagation();
    });
    
    0 讨论(0)
提交回复
热议问题