获得元素dosn值的Javascript工作

时间:2017-03-25 19:28:02

标签: javascript jquery function

我试图通过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;的价值?一旦我改变了吗?

1 个答案:

答案 0 :(得分:3)

您实际上是设置了innerHTML元素的#demo属性,而不是value

&#13;
&#13;
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;
&#13;
&#13;