我试图通过javascript获取元素的值,但即使我知道它不是,我也未定义它的值。
<!DOCTYPE html>
<html>
<body>
<p>Click "Try it" to display the first array element, after a string split.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = "a,b,c,d,e,f";
var arr = str.split(",");
document.getElementById("demo").innerHTML= arr[0];
alert(document.getElementById("demo").value);
}
</script>
</body>
</html>
我如何获得&#34; demo&#34;的价值?一旦我改变了吗?
答案 0 :(得分:3)
您实际上是设置了innerHTML
元素的#demo
属性,而不是value
。
function myFunction() {
var str = "a,b,c,d,e,f";
var arr = str.split(",");
document.getElementById("demo").innerHTML = arr[0];
alert(document.getElementById("demo").innerHTML);
}
&#13;
<p>Click "Try it" to display the first array element, after a string split.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
&#13;