html中的readonly属性与removeAttribute不一致

时间:2016-08-17 04:54:14

标签: javascript html internet-explorer internet-explorer-11

我正在尝试禁用自动填充提示我在页面上输入用户名和密码字段,并且在我在现有代码中引入以下行之后这样做非常成功。 readonly onfocus="myFunc(this.id)。虽然我最初尝试autocomplete="false"autocomplete="off"没有任何帮助,但我只是让它在那里。

<tr>                                                              
   <td align="right">Username:</td>
   <td><input type="text" name="username" class="input" MAXLENGTH=8 size="25" autocomplete="false" id="username " readonly onfocus="myFunc(this.id)" onKeyPress="return submit(event)"></td>
</tr>   

关键是上面的代码确实有助于在大多数浏览器中禁用自动填充功能,但是当我去单击空文本框输入用户名时,文本框为空,不带任何键盘输入(好像它仍然是readonly - 仅在IE11中看到的问题)。点击时会应用颜色变化。调用的JS函数如下所示。

function myFunc(x) {
  //x.removeAttribute('readonly'); 
  //x.style.background = "yellow";
  document.getElementById(x).removeAttribute("readonly"); 
  document.getElementById(x).style.background = "yellow";
}

现在在文本框中再次单击将允许我输入。有人可以告诉我如何在IE中修复此行为。

1 个答案:

答案 0 :(得分:1)

这已经在这里得到解答:Readonly input box bug in Internet Explorer

  

尝试document.getElementById(x).select(),它应该将光标返回输入。