为什么 React 中的 <textarea> 接受值属性,而 HTML 中不接受?
Why does <textarea> in React accept a value attribute but in HTML, it does not?
不确定这是否是 React 特定的,但是,为什么以下内容在 React 中起作用并向元素呈现一些文本:
<textarea value="Some text."></textarea>
但在普通的 HTML 中没有:
<textarea value="Some text."></textarea>
也许我遗漏了什么或做了什么蠢事?提前致歉并致谢!
在HTML中,text
区域是一个non-self-closing标签并且有内容。它通过 children.
定义其文本
<textarea id="story" name="story" rows="5" cols="33">
It was a dark and stormy night...
</textarea>
在 React 中它使用 value
属性。请注意,这也是 self-closing 并且没有 children.
这使 textarea
元素与 input
和 select
等其他表单元素保持一致。
<textarea
id="story"
name="story"
rows="5"
cols="33"
value="It was a dark and stormy night..."
/>
textarea
子内容表示文本区域的默认值,但value
属性表示当前值。
是需要通过JS操作的current值,不是默认值
不确定这是否是 React 特定的,但是,为什么以下内容在 React 中起作用并向元素呈现一些文本:
<textarea value="Some text."></textarea>
但在普通的 HTML 中没有:
<textarea value="Some text."></textarea>
也许我遗漏了什么或做了什么蠢事?提前致歉并致谢!
在HTML中,text
区域是一个non-self-closing标签并且有内容。它通过 children.
<textarea id="story" name="story" rows="5" cols="33">
It was a dark and stormy night...
</textarea>
在 React 中它使用 value
属性。请注意,这也是 self-closing 并且没有 children.
这使 textarea
元素与 input
和 select
等其他表单元素保持一致。
<textarea
id="story"
name="story"
rows="5"
cols="33"
value="It was a dark and stormy night..."
/>
textarea
子内容表示文本区域的默认值,但value
属性表示当前值。
是需要通过JS操作的current值,不是默认值