IE readonly textarea问题 [英] IE readonly textarea problem

查看:168
本文介绍了IE readonly textarea问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我动态更改其readonly属性时,我在IE7和IE8(但不是其他浏览器)中看到了textarea的问题。 textarea最初定义为只读,当用户在文本框内单击时,我将readOnly设置为false。此时,如果我键入任何普通字符,它们就不会显示 - 事实上,文本框的行为就像它仍然是只读的(即箭头键移动,点击删除转到上一页,等等。 )

I'm seeing an issue in IE7 and IE8 (but not other browsers) with a textarea when I dynamically change its "readonly" attribute. The textarea is initially defined as being read-only, and when the user clicks inside the textbox I set readOnly to false. At this point, if I type any normal characters, they don't get displayed - in fact, the text box acts like it is still read-only (i.e., arrow keys move around, hitting Delete goes to the previous page, etc.)

如果我再次点击textarea,输入正常。

If I click inside the textarea again, typing works normally.

这是一个代码片段,用于说明问题:

Here's a code snippet that illustrates the problem:

<html>
<head></head>
<body>
    <textarea id="txt1" onclick="document.getElementById('txt1').readOnly = false" readonly=true>Click in here and try typing.</textarea>
</body>
</html>

我尝试了不同的doctypes。我尝试在点击处理程序中手动调用focus()和click()。我已经尝试设置一个计时器来设置readOnly标志。我尝试过使用setAttribute()/ removeAttribute()。任何这些方法都不会令人高兴。

I've tried different doctypes. I've tried manually calling focus() and click() in the click handler. I've tried setting a timer to set the readOnly flag. I've tried using setAttribute() / removeAttribute(). No joy with any of those approaches.

更新

我最终使用contentEditable,但仅适用于IE - 我仍然使用readOnly用于FF和Safari / Chrome,因为contentEditable似乎不适用于那些浏览器。我也必须重新检查IE9。

I ended up using contentEditable, but only for IE - I'm still using readOnly for FF and Safari/Chrome, because contentEditable seemed to not work for those browsers. I'll have to recheck with IE9, too.

推荐答案

这是按预期工作(由Microsoft)。这将需要两次点击才能开始输入,因为在第一次点击时它仍然是只读的,因此元素不会聚焦。您需要在更改只读后使用Javascript关注元素,否则您的用户必须双击。这是因为即使您无法输入,IE也不会关注已禁用的元素,Chrome会这样做。

That's working as intended (by Microsoft). It's going to take two clicks to start typing because on the first click it's still read only, so the element wouldn't focus. You need to either focus the element with Javascript after changing read only, or your users will have to double click. This is because IE doesn't focus the disabled element, Chrome does, even though you can't type.

在这种情况下,IE似乎不喜欢 .focus(); ,但 .select(); 有效。

IE doesn't appear to like .focus(); in this case, but .select(); works.

document.getElementById('yourTextareaId').onclick = function() {
    this.readOnly = false;
    this.select();
}

这篇关于IE readonly textarea问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆