为什么 innerText,innerHTML 属性 对 javascript 中的输入标签不起作用?
Why innerText,innerHTML property doesnt work on input tags in javascript?
所以我偶然发现了以下代码
<!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
因为inner
指的是元素开始标签和结束标签“之间”的内容。例如:
<p>This is the "innerText" or "textContent"</p>
<input>
元素没有结束标签,因此它永远不能包含任何“内部”内容。对于这些元素,使用 .value
属性 访问它们的内容。
还有其他元素没有结束标记,但在以下情况下,元素没有 innerText
或 .value
因为它们不是为了存储任何数据而设计的,仅对 HTML:
产生语义或结构影响
<area>
<base>
<br>
<col>
<colgroup>
当跨度存在时
<command>
<embed>
<hr>
<img>
<input>
<keygen>
<link>
<meta>
<param>
<source>
<track>
<wbr>
所以我偶然发现了以下代码
<!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
因为inner
指的是元素开始标签和结束标签“之间”的内容。例如:
<p>This is the "innerText" or "textContent"</p>
<input>
元素没有结束标签,因此它永远不能包含任何“内部”内容。对于这些元素,使用 .value
属性 访问它们的内容。
还有其他元素没有结束标记,但在以下情况下,元素没有 innerText
或 .value
因为它们不是为了存储任何数据而设计的,仅对 HTML:
<area>
<base>
<br>
<col>
<colgroup>
当跨度存在时<command>
<embed>
<hr>
<img>
<input>
<keygen>
<link>
<meta>
<param>
<source>
<track>
<wbr>