我想根据用户是否关注输入来切换输入的可见性。我希望允许用户能够编辑输入,当他们离开时,我希望在输入中显示他们刚刚编辑的输入,这将“格式化”编辑后的输入。
就我而言,我有两个输入元素。当用户离开“值”输入时,应显示“显示”输入。下次用户给出“显示”输入焦点时,应该给出“值”输入焦点,以便用户可以输入值。
我整理了一个jsFiddle 。错误是,如果您给第一个输入一个值,单击“虚拟输入”以失去当前输入的焦点,然后再次单击第一个输入,就好像第一个输入从未获得焦点一样。 (请注意,单击后您将无法立即在键盘上键入任何内容)。再次强调,这只是 IE 中的问题。
注意:如果您使用 Tab 键,而不是在导航离开后单击第一个输入,则它会起作用。似乎只是点击的问题。
<div>
<label>Give me a value then give the value focus</label>
<input id="valueInput" type="text" />
<input id="displayInput" type="text" style="display:none;" />
<br />
<label>Dummy Input</label>
<input type="text" />
</div>
$(window.document).on('blur', "#valueInput", function (e) {
$(this).hide();
$("#displayInput").show();
});
$(window.document).on('focus', "#displayInput", function (e) {
$("#valueInput").show().focus();
$(this).hide();
});
$("#valueInput").focus();
jQuery 版本 1.7.2
最佳答案
根据 @Pointy 的评论以及其他一些谷歌搜索,我能够通过以下 Javascript 让它工作。
$(window.document).on('blur', "#downPaymentPercent", function (e) {
$(this).hide();
$("#downPaymentPercentDisplay").show();
});
$(window.document).on('focus', "#downPaymentPercentDisplay", function (e) {
var $downPaymentPercent = $("#downPaymentPercent");
$(this).hide();
$downPaymentPercent.show();
// IE work around since the focus call is delayed, set a timed out call
window.setTimeout(function() {
$downPaymentPercent.focus();
// Set cursor to the end of the input
$downPaymentPercent.val($($downPaymentPercent).val());
}, 1);
});
$("#downPaymentPercent").focus();
这是工作 jsFiddle
关于javascript - IE - 隐藏和显示输入重新显示输入时不会获得焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14348839/