Jos*_*son 11 html javascript jquery contenteditable
目标:将一个keydown事件处理程序附加到一个可信任的跨度,该跨度是一个可信任的div的子级.
问题:如果您键入跨度,则触发父事件而不是子项.我想要的是孩子触发所以我可以抓住文本.我只想要那个有满足感的孩子,因为会有很多孩子.
HTML/JS在下面,小提琴链接在这里:http://jsfiddle.net/aBYpt/4/
<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>
Run Code Online (Sandbox Code Playgroud)
$(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
});
Run Code Online (Sandbox Code Playgroud)
**注意:事件处理委托给文档,因为元素是动态添加的.
所以这是一个错误.解决方法确认FF23和CHROME29(在没有vm的linux上,因此无法测试IE).你必须将包装跨度设置为contenteditable false,你不能只是省略声明contenteditable属性,这是荒谬的.嵌套内容可编辑(jQuery)上的Keypress事件解决方案
这是小提琴:http://jsfiddle.net/aBYpt/14/
<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>
Run Code Online (Sandbox Code Playgroud)
$(document).on( 'keyup', '.parent', function() {
//do stuff
$('#console').html( 'Parent keyup event fired.' );
});
$(document).on( 'keyup', '.child', function(e) {
//do stuff
$('#console').html( 'Child keyup event fired.' );
e.stopPropagation();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9798 次 |
| 最近记录: |