admin管理员组文章数量:1406740
Provided an input element in a DOM, it does have a textContent
property along with value
. It is well known that the value of the input is what shown in the text box, and this element cannot have any children, that is <input>something</input>
still gives an empty input followed by a sibling text node, whereas closing tag is pletely ignored. But when we set a value to the textContent
of this input, it is somehow survives the round-trip:
input.textContent = 'something'
console.log(input.textContent) // this works
Also, after setting the property, the input appear as having child elements in the inspector:
Maybe it's just me, but I fail to see any logical consistency here. Wouldn't it be nice to have a Type Error on setting text content to the input?
Is there any reasoning behind present behaviour?
Provided an input element in a DOM, it does have a textContent
property along with value
. It is well known that the value of the input is what shown in the text box, and this element cannot have any children, that is <input>something</input>
still gives an empty input followed by a sibling text node, whereas closing tag is pletely ignored. But when we set a value to the textContent
of this input, it is somehow survives the round-trip:
input.textContent = 'something'
console.log(input.textContent) // this works
Also, after setting the property, the input appear as having child elements in the inspector:
Maybe it's just me, but I fail to see any logical consistency here. Wouldn't it be nice to have a Type Error on setting text content to the input?
Is there any reasoning behind present behaviour?
Share asked May 10, 2016 at 15:39 SchmoSchmo 3532 silver badges11 bronze badges1 Answer
Reset to default 3The DOM is not HTML, but an API for tree structures that ply with the XML information set Translating <input>something</input>
into an input element followed by a text node is the behaviour of the HTML parser, not the behaviour of the DOM.
In fact, if you use XHTML, served as application/xhtml+xml
, <input>something</input>
will bee an input element with a text node child, the same as using .textContent
. So it's entirely normal that .textContent
works the way it does.
本文标签: javascriptWhat is the purpose of textContent in an input elementStack Overflow
版权声明:本文标题:javascript - What is the purpose of textContent in an input element? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744850830a2628484.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论