innerHTML/innerText/value的区别
value
是指用 <input value="xxxxx">
或者<textarea>xxxxxx</textarea>
输入的值(任何输入的值,数字或字符)。
innerHTML
指在HTML元素内的内容。
innerText
指在HTML元素内的文字内容,会自动忽略标签。
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function getValue1()
{
var x=document.getElementById("1")
alert(x.innerHTML)
}
function getValue2()
{
var x=document.getElementById("2")
alert(x.innerHTML)
}
function getValue3()
{
var x=document.getElementById("3")
alert(x.value)
}
function getValue4()
{
var x=document.getElementById("4")
alert(x.value)
}
function getValue5()
{
var x=document.getElementById("5")
alert(x.value)
}
function getValue6()
{
var x=document.getElementById("6")
alert(x.innerText)
}
</script>
</head>
<body>
<h1 id="/1" onclick="getValue1()">This is a header</h1>
<div id="2" onclick="getValue2()">
<span class="hello">
All tags and their children are include in innerHTML.
</span>
All this is part of innerHTML.
</div>
<br/><br>
<textarea id="3" onclick="getValue3()">数字123</textarea>
<br/><br>
<input id="4" onclick="getValue4()" value="1"/>
<br/><br>
<input id="5" onclick="getValue5()" value="一"/>
<br/><br>
<div id="6" onclick="getValue6()">
This is for testing <code>innerHTML</code> and <strong>innerTEXT</strong>.
</div>
</body>
</html>