如何在jQuery中将以下代码与委托结合使用?
我在所有可编辑元素周围都有一个#commentContainer
,并且我动态地添加了可编辑字段(Jeditable)。编辑功能不适用于动态加载的项目。
/* Bind Jeditable instances to "edit" event. */
$(".edit").editable('/Comment/PostComment/', {
type: 'textarea',
cancel: 'Cancel',
submit: 'OK',
indicator: '<img src="img/indicator.gif">',
tooltip: 'Click to edit...',
event: "edit"
});
/* Find and trigger "edit" event on correct Jeditable instance. */
$(".edit_trigger").bind("click", function () {
$(this).prev().trigger("edit");
});
发布于 2011-01-16 23:02:24
当一个元素被添加到DOM中时,事件委托不适用于运行的代码。一些浏览器事件需要首先发生,比如点击。
因此,如果您添加的新元素应该运行editable
插件,则需要在添加它们时手动调用它。
$('<textarea>').editable( /* settings */ )
.appendTo( '#commentContainer' );
发布于 2012-10-11 14:49:10
对于动态添加的字段,有一个解决方案是添加jQuery.editable。
$("body").on("click",".editable",function(e){
// Add editable plugin
// but! for `focus` instead common `clik` event
$(this).editable('go.to',
{
event : 'focus.editable',
..
..
// Then trigger focus event
}).trigger("focus");
})
https://stackoverflow.com/questions/4708647
复制相似问题