Javascript blur()和keypress()无法使用replacewith()处理JQuery

Javascript blur()和keypress()无法使用replacewith()处理JQuery,javascript,jquery,Javascript,Jquery,如何在JQuery中将blur()和keypress()事件与replaceWith()一起使用。 我在下面尝试了这段代码,它使用blur()时没有任何错误,只是触发了一个单词“undefined”,但当我按enter键时,出现了以下错误: 未捕获的DomeException:未能在“节点”上执行“replaceChild”:要删除的节点不再是此节点的子节点。可能是在“模糊”事件处理程序中移动的 这是我的代码: $('body').on('blur keypress', 'input', fun

如何在JQuery中将blur()和keypress()事件与replaceWith()一起使用。 我在下面尝试了这段代码,它使用blur()时没有任何错误,只是触发了一个单词“undefined”,但当我按enter键时,出现了以下错误:

未捕获的DomeException:未能在“节点”上执行“replaceChild”:要删除的节点不再是此节点的子节点。可能是在“模糊”事件处理程序中移动的

这是我的代码:

$('body').on('blur keypress', 'input', function (e) {
  if (e.type === 'focusout' || e.keyCode === 13) {
    $(this).replaceWith('<strong>' + $(this).val() + '</strong>');
  }
});
$('body')。打开('blur keypress','input',函数(e){
if(e.type=='focusout'| | e.keyCode===13){
$(this.replace为(''+$(this.val()+'');
}
});

一旦按下enter键,元素就会失去焦点,即当它被移除时会模糊,并触发新事件,但一旦触发,元素就不再存在,因此出现错误

您可以通过拆分条件来修复它,并在按下enter键时触发
blur
事件

$('body').on('blur keypress', 'input', function (e) {
  if ( e.type === 'focusout' ) {
    $(this).replaceWith('<strong>' + $(this).val() + '</strong>');
  }
  if ( e.keyCode === 13 ) {
    $(this).trigger('blur')
  }
});
$('body')。打开('blur keypress','input',函数(e){
如果(e.type==='focusout'){
$(this.replace为(''+$(this.val()+'');
}
如果(如keyCode===13){
$(this.trigger('blur'))
}
});