为什么innerText,innerHTML属性不适用于javascript中的输入标签?

Sha*_*hul 0 javascript attributes innertext

所以我偶然发现了以下代码

<!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>
Run Code Online (Sandbox Code Playgroud)

之前在 javascripte 中没有使用过 .value,我想用 insideText 属性替换它,但它根本不起作用!

我有一个想法,也许<input/>是一个自闭合标签,这就是为什么 innerText 无法在它之间插入的原因?因此,要在元素之间插入至少应该有 2 个标签

那么有人可以巩固这一点吗?或者如果不是那么为什么 .innerText 在这里不起作用而不是 .value

Sco*_*cus 6

因为inner指的是元素的开始标签和结束标签“之间”的内容。例如:

<p>This is the "innerText" or "textContent"</p>
Run Code Online (Sandbox Code Playgroud)

<input>元素没有结束标记,因此它永远不能包含任何“内部”内容。对于这些元素,使用.value属性来访问它们的内容。

还有其他元素没有结束标记,但在以下情况下,这些元素没有innerText.value因为它们不是为存储任何数据而设计的,仅对 HTML 具有语义或结构效果:

  • <area>
  • <base>
  • <br>
  • <col>
  • <colgroup>当跨度存在时
  • <command>
  • <embed>
  • <hr>
  • <img>
  • <input>
  • <keygen>
  • <link>
  • <meta>
  • <param>
  • <source>
  • <track>
  • <wbr>