0

如果我有以下 HTML

    <input type="text" name="Text1" value="MyText" id="Text1"/>

如果我在页面后运行以下 javascript 代码

var text = document.getElementById("Text1");
text.defaultValue = "i am default value";

which sets the defaultValue of the input field I get two different reactions depending on the browser. In IE9, the value is not changed, but default value is changed, which is actually the expected behaviour. In Chrome (and Firefox) both the value and defaultValue are changed. WHY? However if I run this JavaScript

var text = document.getElementById("Text1");
text.value = text.value; // No functional change, however triggers something in the DOM
text.defaultValue = "i am default value";

then it works as expected, that is it is only the defaultValue that is changed in Chrome

Any ideas on why and how to make Chrome (and Firefox) run as IE9 does?

4

1 回答 1

0

defaultValue通常是一个元素的值,直到用户与该元素交互。一旦用户与元素进行交互,value属性将不会随着defaultValue属性的改变而改变。当在用户交互之前更改 defaultValue 属性时,大多数浏览器都会更新元素的 value 属性。这种行为在浏览器之间可能不一致。

于 2016-01-21T09:50:30.043 回答