【发布时间】:2025-09-16 04:55:01
【问题描述】:
所以我偶然发现了以下代码
<!DOCTYPE html>
<html>
<body>
Name: <input type="text" id="myText" value="Mickey">
<p>Click the button to change the value of the text field.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
document.getElementById("myText").value = "Johnny Bravo";
}
</script>
</body>
</html>
之前没有在 javascripte 中使用过 .value,我想用 innerText 属性代替它,但它根本不起作用!
我有一个想法,可能<input/> 是一个自闭合标签,这就是为什么 innerText 不能在它之间插入?因此,在元素之间插入至少应该有 2 个标签
所以任何人都可以巩固这一点吗?或者如果不是,那么为什么 .innerText 不能在这里工作而不是 .value
【问题讨论】:
-
请注意,将 id 分配给元素会在 HTML5 中创建 window property of the same name。设置现有元素的
id属性还会创建同名的窗口属性。 (这是您之前提出并回答的已删除问题的正确答案 here。)
标签: javascript attributes innertext