我有以下代码可以启用内联编辑
$('.edit_td').click(function() {
resetEditedCells();
$(this).addClass('active').html('<input type="text" value="' + $(this).html() + '">');
});
function resetEditedCells() {
$('.edit_td.active').html(function() {
return $(this).find('input').val();
});
}
从 td 切换到 td 时效果很好。但是,当我单击表外的其他任何位置时,我希望输入字段转换回常规 tds。
我尝试过这样的:
$(document).on('blur', function(e) {
if($('.edit_td').hasClass('active')) {
$('.active').remove();
} else {
alert("false");
}
});
.blur() 未触发。有人吗?
最佳答案
尝试类似的方法来替换您拥有的代码
$(document).on('click', function(e) {
var edit_td = $(e.target).closest('.edit_td');
if ( edit_td.length > 0 ) {
resetEditedCells();
edit_td.addClass('active')
.html('<input type="text" value="' + edit_td.html() + '">');
}else{
$('.edit_td.active').removeClass('active').empty();
}
});
这使用 closest()
来确定点击是来自 TD 内部还是外部。
关于javascript - 单击元素外部时重置输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23883809/