为什么我的 JavaScript 无法加载到我的网页(使用 window.onload)?
Why won't my JavaScript load on my webpage (using window.onload)?
我正在尝试使用实时预览功能(我在本地服务器上查看网页)在 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
我正在尝试使用实时预览功能(我在本地服务器上查看网页)在 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