gpt4 book ai didi

javascript - contenteditable 元素的子元素的事件

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:34:44 29 4
gpt4 key购买 nike

我有

myDiv.bind('keypress', '> *', function(event) { console.log('keypress') });

但是好像不行。

myDiv 是 contenteditable,我正在尝试访问正在编辑的 p 元素。

http://jsfiddle.net/nb5UA/5/ (尝试在点击回车后创建的 div 中输入)

最佳答案

This is barely possible with contenteditable seeing as the elements do not hold input like events and therefore do not have real focus, so you can't actually determine the event.target. The event.target will always be the container that has the attribute contenteditable="true".

However you can use the DOMCharacterDataModified event like the example & demo below.

$('#test').on('DOMCharacterDataModified',  function( event ) {
if($(event.target).parent().attr('id') === 'test') { // Reference 1
alert('modified');
}
});

演示:http://jsfiddle.net/nb5UA/15/

Reference 1: The if statement is checking that the event.target is a direct child of the #test container.

浏览器对 DOMCharacterDataModified 的支持还不错。 不支持,我找不到太多关于该事件的信息,所以如果有人有好的资源,请在评论中分享。

关于javascript - contenteditable 元素的子元素的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18169626/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com