给出input.value和input.textContent之间的区别。为什么用一个代替另一个? [英] Give the difference between input.value and input.textContent. Why is one used instead of the other?

查看:231
本文介绍了给出input.value和input.textContent之间的区别。为什么用一个代替另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么使用 input.value 代替 input.textContent 。两者之间有什么区别?



例如,如果我想从输入框中检索内容。

 < input type = number> 

我必须使用此代码

  var input = document.querySelector( input); 

input.value

而不是这个

  input.textContent 

只是想对每个对象有一个更清晰的了解。

解决方案

来自 MDN


[...] textContent 返回每个子节点的 textContent 的串联,不包括注释和处理指令。如果节点没有子节点,则为空字符串。


本质上来说, textContent 为您提供节点包含的文本表示形式。将其视为开头和结尾标签之间的所有内容,例如



 控制台.log(document.querySelector('span')。textContent);  

 < span>此文本< / span>但不是这个 c / c> 



< input> ; 元素,但是不能有子元素 (内容模型:无)。与它们关联的值只能通过 value 属性访问。


Why is it that input.value is used instead of input.textContent. What is the difference between both?

for example, if I want to retrieve content from an input box.

<input type="number">

I have to use this code

var input = document.querySelector("input");

input.value

instead of this one

input.textContent

Just want to get a clearer understanding of each.

解决方案

From MDN:

[...] textContent returns the concatenation of the textContent of every child node, excluding comments and processing instructions. This is an empty string if the node has no children.

Essentially, textContent gives you a textual representation of what a node contains. Think of it as being everything between the opening and closing tags, e.g.

console.log(document.querySelector('span').textContent);

<span> this text </span> but not this one

<input> elements however cannot have children (content model: nothing). The value that is associated with them can only be accessed via the value property.

这篇关于给出input.value和input.textContent之间的区别。为什么用一个代替另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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