不确定如何解决这个问题。
我正在使用jQuery.focus()
和jQuery.focusout()
来触发Ajax请求并重命名WordPress中的帖子。这段代码在Chrome和Firefox中运行正常,但我不能让它在IE上运行。
片段:
$('.editname').on('click', function() {
var input = $('<input id="editcat" type="text" name="editcat" value="' + name + '" />');
input.appendTo($(this));
$("#editcat").focus();
});
$(".editname").focusout(function() {
$(this).children('input').detach();
$(this).children('span').show();
});
工作示例: http://jsfiddle.net/moraleida/fE5Tq/3/
似乎在IE中的appendTo
之后立即调用focusout()事件,之后浏览器在附加输入上有时间focus()
。
是否有针对此的已知解决方法?
修改
将focusout
更改为blur
不起作用。显然,问题是调用$("#editcat").focus();
使.editname
松散焦点/模糊。如果我评论该行,则输入显示正常,但是当我单击以聚焦它时,它会分离。
答案 0 :(得分:2)
尝试使用模糊事件
$(".editname").on('blur', function() {
$(this).children('input').detach();
$(this).children('span').show();
});
答案 1 :(得分:2)
事实证明,问题在于将focusout
附加到父元素而不是input
元素。这解决了它:
$(".editname").on('focusout', 'input', function() {
// $(this) now refers to the input element, not .editname
$(this).siblings('span').show();
$(this).detach();
});
});
最后的工作小提琴供参考:http://jsfiddle.net/moraleida/fE5Tq/8/
答案 2 :(得分:1)
尝试使用模糊事件而不是焦点。