javascript - IE - 隐藏和显示输入重新显示输入时不会获得焦点

标签 javascript jquery

我想根据用户是否关注输入来切换输入的可见性。我希望允许用户能够编辑输入,当他们离开时,我希望在输入中显示他们刚刚编辑的输入,这将“格式化”编辑后的输入。

就我而言,我有两个输入元素。当用户离开“值”输入时,应显示“显示”输入。下次用户给出“显示”输入焦点时,应该给出“值”输入焦点,以便用户可以输入值。

我整理了一个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/

相关文章:

php - jQuery POST 表单数据行

jquery - 使用 $(this).addClass 在 AJAX 成功中不起作用

javascript - 从加载的页面调用 js 页面

具有多个复选框的 jQuery 过滤器

javascript - 通过 jQuery 返回 JSON 响应并重构 HTML 表

javascript - 我怎样才能使这段代码更好地工作?

javascript - 如何从 iframe 读取父页面的页面标题?

javascript - 将表单转换为查询字符串,Vanilla JavaScript

javascript - 当页面滚动时,打开我的侧边抽屉会导致固定位置的标题移动

javascript - jQuery 菜单显示问题