目标:将keydown事件处理程序附加到作为可填写div的子代的可满足范围.
问题:如果您输入跨度,父事件不会被触发.我想要的是孩子触发,所以我可以抓住文本.我只想要这个可爱的孩子,因为会有很多.
HTML / JS在下面,小提琴链接在这里:http://jsfiddle.net/aBYpt/4/
HTML
<div class="parent" contenteditable="true"> Parent div text. <span class="child" contenteditable="true">First child span text.</span> <span class="child" contenteditable="true">Second child span text.</span> <span class="child" contenteditable="true">Third child span text.</span> </div> <div id="console"></div>
的JavaScript / jQuery的
$(document).on( 'keyup','.parent',function() { $('#console').html( 'Parent keyup event fired.' ); //do stuff }); $(document).on( 'keyup','.child',function() { $('#console').html( 'Child keyup event fired.' ); //do stuff });
**注意:事件处理被委派为文档,因为元素被动态添加.
解决方法
所以这是一个bug.解决方法确认FF23和CHROME29(在没有vm的linux上,所以不能测试IE).您必须将包装跨度设置为contenteditable false,您不能仅仅省略声明contenteditable属性,这是非常可靠的.解决方案通过
Keypress event on nested content editable (jQuery)
这是小提琴: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',function() { //do stuff $('#console').html( 'Parent keyup event fired.' ); }); $(document).on( 'keyup',function(e) { //do stuff $('#console').html( 'Child keyup event fired.' ); e.stopPropagation(); });
原文地址:https://www.jb51.cc/js/152953.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。