输入元素中textContent的目的是什么? [英] What is the purpose of textContent in an input element?

查看:68
本文介绍了输入元素中textContent的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在DOM中提供了输入元素,它确实具有 textContent 属性以及 value .众所周知,输入的值是文本框中显示的值,并且此元素不能有任何子元素,即< input> something</input> 仍会给出一个空的输入通过同级文本节点,而结束标记将被完全忽略.但是,当我们为该输入的 textContent 设置一个值时,它就可以在往返过程中幸存下来:

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 completely 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?

推荐答案

DOM不是HTML,而是用于符合

The DOM is not HTML, but an API for tree structures that comply 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.

实际上,如果您使用XHTML作为 application/xhtml + xml ,则< input> something</input> 将成为带有文本的输入元素节点子节点,与使用 .textContent 相同.因此, .textContent 的工作方式是完全正常的.

In fact, if you use XHTML, served as application/xhtml+xml, <input>something</input> will become 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.

这篇关于输入元素中textContent的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆