我正在尝试使用Live Preview功能(我在本地服务器上查看网页)在Adobe Dreamweaver中开发HTML页面,而我的JavaScript不够灵活。我试图用一个JS数组填充一个下拉菜单,并且我之前的测试工作了,但是现在当我尝试让脚本自动运行时,a)没有任何反应或b)按钮闪烁,表明已更新,但仍显示空白。任何帮助,将不胜感激!
<script>
function start() {
var arrOptions = []; //arrOptionsCollection with HTML tags
var arrOptionsCollection = [0, 1, 2]; //Values for dropdown
arrOptions.push("<option value='Select From List...'>Select From List...</option>") //Dropdown Default
//Adds HTML tags to arrOptionsCollections
for (var i=0, n = arrOptionsCollection.length; i < n; i++) { =
arrOptions.push("<option value='" + arrOptionsCollection[i] + "'>" + arrOptionsCollection[i] + "</option>");
}
//Transfers arrOptions to actual HTML
document.getElementById("district-select").innerHTML = arrOptions.join();
}
document.getElementById("form-select").onload = function() {start()};
</script>
<body onload="start();">
<form id="form-select">
<select id="district-select">
<!--Script Inserts Options Here-->
</select>
</form>
我不知道=
循环开始时for
的作用是什么。简单来说,这没有任何意义。因此,代替:
for (var i=0, n = arrOptionsCollection.length; i < n; i++) { =
放置:
for (var i=0, n = arrOptionsCollection.length; i < n; i++) {
这是您的代码:
function start() {
var arrOptions = []; //arrOptionsCollection with HTML tags
var arrOptionsCollection = [0, 1, 2]; //Values for dropdown
arrOptions.push("<option value='Select From List...'>Select From List...</option>") //Dropdown Default
//Adds HTML tags to arrOptionsCollections
for (var i=0, n = arrOptionsCollection.length; i < n; i++) {
arrOptions.push("<option value='" + arrOptionsCollection[i] + "'>" + arrOptionsCollection[i] + "</option>");
}
//Transfers arrOptions to actual HTML
document.getElementById("district-select").innerHTML = arrOptions.join();
}
document.getElementById("form-select").onload = function() {start()};
<body onload="start();">
<form id="form-select">
<select id="district-select">
<!--Script Inserts Options Here-->
</select>
</form>
这里是现场演示:https://codepen.io/marchmello/pen/yLYbYjw?editors=1010