为什么内部HTML不适合这个javascript?

时间:2015-07-03 18:18:01

标签: javascript

我做错了以下是不行的:

<div id="devtest">test</div>

<script>
var myarray = ["a", "b", "c"];

var somediv = document.getElementById('#devtest')
for (i = 0; i < myarray.length; i++) { 
 somediv.innerHtml += myarray[i] + "<br>";
}
</script>

http://jsfiddle.net/avg5ffj0/1/

3 个答案:

答案 0 :(得分:3)

有两个错误innerHTMLdocument.getElementById('devtest')没有#

var myarray = ["a", "b", "c"];

var somediv = document.getElementById('devtest')
for (i = 0; i < myarray.length; i++) { 
 somediv.innerHTML += myarray[i] + "<br>";
}

DEMO

答案 1 :(得分:2)

&#34; id&#34;你的元素是&#34; devtest&#34;,而不是&#34;#devtest&#34;。

哦,它也是innerHTML而不是innerHtml

答案 2 :(得分:0)

代码应为:

测试
<script>
var myarray = ["a", "b", "c"];

var somediv = document.getElementById('devtest')
for (i = 0; i < myarray.length; i++) { 
 somediv.innerHtml += myarray[i] + "<br>";
}
</script>

getElementById只是元素的id(没有任何#符号)