![](/img/trans.png)
[英]jquery/js error - DOM Error: DOM Exception 8 - When I .html any variable
[英]Where is the error [JS DOM HTML]?
我想使用js函数向select添加选项。 但是媒介上有一个问题,我认为有人可以帮助我吗?
<html>
<body onload="myFunction()">
<select id="mySelect"></select>
<script>
function myFunction() {
var months= ["january","february"];
var mySelect = document.getElementById('mySelect');
var newOption = document.createElement('option');
for(var i=0;i<mesi.length;i++){
newOption.innerHtml=mesi(i).valueOf;
mySelect.appendChild(newOption);
}
}
</script>
</body>
</html>
您的代码似乎在for块中具有错误的数组名称。 另外,当您在数组中调用索引时,请使用方括号-months months[i]
而不是括号。 并且不需要使用valueOf。
function myFunction() {
var months= ["january","february"];
var mySelect = document.getElementById('mySelect');
var newOption = document.createElement('option');
for(var i=0;i<months.length;i++){ // <- here is where the fix goes.
newOption.innerHtml=months[i];// <- here is where the fix goes.
mySelect.appendChild(newOption);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.