Pir*_*jan 3 javascript jquery contenteditable mustache
目标:将事件处理程序附加到嵌套在小胡子模板/脚本中的contenteditable div.
问题:我有一个我想要编辑的小胡子js模板.这个想法是,在keyup上它应该触发一个事件(以后用于更新相同的数据服务器端).
该块是可编辑的,但在脚本标记内不会触发可信事件.
我尝试了更多一般的提示,例如: 检测可信孩子的关键字事件,其父母是一个可信的div 和 嵌套内容可编辑(jQuery)上的Keypress事件, 但没有处理模板场景.
HTML
<script id="post" type="text/template">
<div class="editable" contenteditable="true">
<h2>{{bio}}</h2>
<p>{{summary}}</p>
</div>
<span class="child" contenteditable="true">static inside template.</span>
</script>
Run Code Online (Sandbox Code Playgroud)
JQuery的
$('.editable').on('keyup', function() {
alert('editable changed')
});
$('.child').on('click', function() {
alert('static child changed')
});
Run Code Online (Sandbox Code Playgroud)
将事件绑定到绑定时存在于dom中的父级(委托)
$(document.body).on('keyup','.editable', function() {
alert('editable changed')
})
.on('click','.child', function() {
alert('static child changed')
});
Run Code Online (Sandbox Code Playgroud)