【问题标题】:Textarea is not editable (not readonly) in IETextarea 在 IE 中不可编辑(非只读)
【发布时间】:2012-04-06 02:34:09
【问题描述】:

重现问题的最小代码如下:

<div class="cell">
  <input type="text" size=1>
  <textarea style="display:none;"></textarea>
</div>
<script type="text/javascript">
  $('.cell input').focusin(function() {
    $(this).hide();
    $('.cell textarea').show().focus();
  });
</script>

当输入元素被点击时,它应该被隐藏并且文本区域应该被显示和聚焦。这工作正常,但仅在 IE(甚至 IE9)中,textarea 的行为类似于只读,尽管它是可聚焦的并且未设置只读属性。当再次点击 textarea 时,它变为可编辑的。

我也尝试了select() 而不是focus(),正如IE readonly textarea problem 中所建议的那样,但结果没有区别。

我错过了什么?

【问题讨论】:

  • 我刚刚在 Chrome 中进行了测试,它运行良好,但在 IE9 中,就像你描述的那样jsfiddle.net/aUDJn IE 到底是什么?

标签: html internet-explorer textarea


【解决方案1】:

由于某种原因,如果您立即集中注意力,则无法输入。但是,如果您在聚焦之前稍等片刻,它就会起作用。如果你把它集中在 setTimeout(func, 0) 中,它就可以工作。

setTimeout 中使用 0 作为毫秒参数基本上会将函数推到调用堆栈的底部,有时这会修复问题。不太清楚为什么。

编辑:这个问题解释了为什么会这样:Why is setTimeout(fn, 0) sometimes useful?

$('.cell input').focusin(function() {
    $(this).hide();
    $('.cell textarea').show();
    setTimeout(function(){
        $('.cell textarea').focus();
    }, 0);
});​

演示:http://jsfiddle.net/aUDJn/1/

【讨论】:

  • 非常感谢您提供的链接和答案。
猜你喜欢
  • 2011-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-28
  • 2019-07-18
  • 2022-12-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多