我有问题在我使用child onclick onclick.my预期结果禁用时再次启用父级函数。当单击父函数时,它再次工作,但它不起作用。因此,以下是我的代码。
function showUserInfo(ini){
$(ini).on("click", showUserInfo);
$(ini).find('.status').html('<i onClick="recentOpen(this)" class="fa fa-eye eyeMark"></i>');
}
function recentOpen(ini){
$(ini).closest('.userIntro').prop("onclick", null);
$(ini).remove();
}
html代码类似:
<div onClick="showUserInfo(this)" class="userIntro">
<div>Intro</div>
<div class="status"></div>
</div>
不要混合活动处理程序。如果我正确理解,则无需删除任何事件处理程序功能,您只需要防止事件冒泡:
// Attach event handler to userIntro
$('.userIntro').on('click', function(){
// Append FA Icon
$(this).find('.status').html('<i class="fa fa-eye eyeMark">FA</i>');
// Add another handler, this time test whether the target
// is the <i> (using on()) we already appended
}).on('click', '.status > .fa', function(e){
// Prevent the event from bubbling so the previous
// function isn't invoked
e.stopPropagation();
//Remove the icon.
$(this).remove();
});
或在附加事件处理程序附加之前,将事件处理程序附加到图标上:
$('.userIntro').on('click', function(){
var $fa = $('<i class="fa fa-eye eyeMark">FA</i>').click(function(e){
e.stopPropagation();
$(this).remove();
});
$(this).find('.status').append($fa);
});
jsfiddle