javascript - What is the purpose of textContent in an input element? - Stack Overflow

Provided an input element in a DOM, it does have a textContent property along with value.It is well k

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 badges
Add a ment  | 

1 Answer 1

Reset to default 3

The 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744850830a4597116.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信