简体   繁体   English

将keypress和focusout事件附加到contenteditable div中的元素

[英]Attaching keypress and focusout event to elements inside contenteditable div

I want to attach keypress and focusout event handlers to a paragraph inside contenteditable div . 我要附加keypressfocusout事件处理程序CONTENTEDITABLE内一个段落div

The following code doesn't seem to work: 以下代码似乎无效:

<div contenteditable="true">
    <p id="p1">Test</p>
    <p id="p2">Test</p>
    <p id="p3">Test</p>
</div>

$('#p1').bind('keypress', function(e) {
    alert('keypress');
});

$('#p1').focusout(function(e) {
    alert('focusout');
});

JSFiddle JSFiddle

Basically the contenteditable is the one that will respond to events dispatched from the inner Nodes. 基本上, contenteditable可编辑的内容将响应内部节点发出的事件。 If we get the caret position and than target it's range's parentNode - than we can retrieve the desired ID 如果我们获得插入符号的位置,然后将其定位为范围的parentNode,则可以获取所需的ID

 $('[contenteditable]').on('keypress focusout', function(ev) { var node = window.getSelection().getRangeAt(0).commonAncestorContainer; var nodeParent = node.parentNode; // Do something if parent node ID is "p1" if( nodeParent.id === "p1") { console.log( nodeParent.id +' '+ ev.type ); // or whatever you need to do here } }); 
 <div contenteditable="true"> <p id="p1">TestP1 do someting on keypress</p> <p id="p2">TestP2 is Not Interesting</p> </div> <script src="//code.jquery.com/jquery-3.1.0.js"></script> 

There's an answer over here: https://stackoverflow.com/a/18772205/2100255 这里有一个答案: https : //stackoverflow.com/a/18772205/2100255

Basically, you need to wrap your inner contenteditables with non-contenteditables. 基本上,您需要使用非contenteditables包装内部的contenteditables。 The event fires off the node which declares contenteditable=true. 该事件触发声明contenteditable = true的节点。

I have altered the solution, Refer the latest fiddle 我已更改解决方案,请参阅最新的小提琴

$(document).on('keypress', '#p1[contenteditable="true"]', function(e) {
   alert("test");
});

You can do it as below Instead of click event you can change it to event on which you want to track the change.I am using click for example 您可以按以下方式进行操作,而不是单击事件,您可以将其更改为要跟踪更改的事件。

 $("div").click(function(e){ console.log($("#"+$(e.target)[0].id).text()); }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 contentEditable div 中的 Span 标签需要按键事件 - Span tag inside contentEditable div needs keypress event 如何使用&#39;contenteditable&#39;属性将keypress事件添加到div - How to add keypress event to div with 'contenteditable' attribute 如何在 vuejs/vue.js 中 contenteditable true div 内的 span 标签中调用 keypress 事件? - How to call keypress event in span tags inside contenteditable true div in vuejs / vue.js? 如何在设置为contenteditable true的div的子代内部获得焦点或按键事件? - How do you get a focus or keypress event inside the children of a div set to contenteditable true? 在 contenteditable 父 div 中的 div 上附加 keydown 事件侦听器不起作用? - Attaching keydown event listener on div in contenteditable parent div not working? 在可编辑内容的聊天div中触发Enter-Keypress事件 - Fire Enter-Keypress event in contenteditable chat div 中的元素<li>在 contenteditable 中有时不会收到按键事件 - Elements in <li> in contenteditable sometimes don't receive keypress event 排除焦点事件的元素 - Exclude elements for focusout event 使用表单元素-复选框-contenteditable div中 - Using form elements - checkboxes - inside contenteditable div 使用焦点在contenteditable div内的元素? - Using focus on elements inside contenteditable div?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM