Textarea在IE中不可编辑(不是只读)

时间:2012-03-21 14:45:08

标签: html internet-explorer textarea

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

<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>

单击input元素时,应隐藏它,并显示和聚焦textarea。这工作正常,但只有在IE(甚至是IE9)中,textarea的行为类似于readonly,尽管它是可聚焦的,并且readonly属性未设置。再次单击textarea时,它将变为可编辑状态。

我也按IE readonly textarea problem的建议尝试了select()而不是focus(),但结果没有差异。

我错过了什么?

1 个答案:

答案 0 :(得分:4)

出于某种原因,如果您立即关注它,则无法输入。但是,如果你在关注它之前稍等一下,它就可以了。如果你将它集中在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);
});​

DEMO:http://jsfiddle.net/aUDJn/1/