onkeyup, onkeydown events not firing for SPAN element

后端 未结 3 2010
陌清茗
陌清茗 2020-12-20 05:11

I\'ve defined the following two span elements: 1) element inside a contenteditable div. 2) contenteditable element.

So far, I can\'t get the onkey events to fire

3条回答
  •  再見小時候
    2020-12-20 05:51

    You could try using the new window.getSelection() API through which you could access the leaf-most node that is part of your current selection. I.e:

    One Two

    JS:

    function getTarget(ev) {
       const selection = window.getSelection();
       // assuming your cursor is over "One"
       console.log(selection.baseNode.parentNode) // 
    }
    

    May or may not meet your requirements.

提交回复
热议问题