javascript - Events on children of contenteditable element - Stack Overflow

I have myDiv.bind('keypress', '> *', function(event) { console.log('keypres

I have

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

but it does not seem to work.

myDiv is contenteditable and I am trying to get access to p elements that are being edited.

/ (try typing within the div created after you click enter)

I have

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

but it does not seem to work.

myDiv is contenteditable and I am trying to get access to p elements that are being edited.

http://jsfiddle/nb5UA/5/ (try typing within the div created after you click enter)

Share edited Aug 11, 2013 at 8:00 iConnor 20.2k14 gold badges66 silver badges97 bronze badges asked Aug 11, 2013 at 7:09 marcopolomarcopolo 2,0454 gold badges19 silver badges32 bronze badges 1
  • Post a demo. Your code should work. – Blender Commented Aug 11, 2013 at 7:12
Add a ment  | 

3 Answers 3

Reset to default 7

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');
    }
});

Demo: http://jsfiddle/nb5UA/15/

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

The browser support for DOMCharacterDataModified is not bad. < IE9 is not supported, and I can't find much info on the event so if anyone has a good resource for it, please share in the ments.

The issue may not be the selector, but rather that the <div id="test"> is always the event.target.

$('#test').on('keypress', function (e) {
    console.log(e.target);
});

With this, at least in Chrome, the console just logs:

<div id="test" contenteditable="true" style="width:500px; overflow: scroll; height: 500px; border: 1px solid #000;">...</div>
<div id="test" contenteditable="true" style="width:500px; overflow: scroll; height: 500px; border: 1px solid #000;">...</div>
<div id="test" contenteditable="true" style="width:500px; overflow: scroll; height: 500px; border: 1px solid #000;">...</div>
<div id="test" contenteditable="true" style="width:500px; overflow: scroll; height: 500px; border: 1px solid #000;">...</div>
<div id="test" contenteditable="true" style="width:500px; overflow: scroll; height: 500px; border: 1px solid #000;">...</div>
<div id="test" contenteditable="true" style="width:500px; overflow: scroll; height: 500px; border: 1px solid #000;">...</div>
...

Keypress event will bubble up to the container element, so you don't necessarily need bind to the children. Instead bind only to the container, than you can check which element was edited by accessing to the event.target property.

$('#container').on('keypress', function (event) { alert(event.target) });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744856745a4597451.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信