“.innerHTML”之间的区别和“.value”在JS中 [英] Difference between ".innerHTML" and ".value" in JS
问题描述
我对JavaScript中的 .innerHTML
和 .value
之间的区别感到困惑。这是我的代码:
I am confused on what is the difference between .innerHTML
and .value
in JavaScript. Here is my code:
<body>
Input string: <input type="text" id="input" />
....
</body>
当我使用这段代码时,我无法获得输入字符串的内容:
When I use this code I cannot get the content of input string:
var str=document.getElementById("input").innerHTML;
虽然我使用以下代码,但它可以工作:
While I use the following code, it works:
var str=document.getElementById("input").value;
任何人都知道它们之间的区别是什么?
Any one knows what is the difference between them?
推荐答案
value
指的是输入元素(或textearea)的值
value
refers to the value of an input element (or textearea)
<input value="hello world">
值将是hello world
(或者在里面键入的任何值)
value would be "hello world"
(or any value typed inside)
innerHTML
指的是内容在HTML元素中。
innerHTML
refers to the contents inside an HTML element.
<div>
<span class="hello">
All tags and their children are include in innerHTML.
</span>
All this is part of innerHTML.
</div>
div标签的innerHTML将是字符串:
innerHTML of the div tag would be the string:
'<span class="hello">
All tags and their children are include in innerHTML.
</span>
All this is part of innerHTML.'
这篇关于“.innerHTML”之间的区别和“.value”在JS中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!