【发布时间】:2016-03-17 21:38:57
【问题描述】:
我的应用程序在 IE9 之前运行良好,现在当我将它升级到 IE11 时它停止运行。这是一个示例 html 和 js。
问题
在第一次运行时,即。 加载时效果很好。但是当我选择 PDA 品牌时,第二个下拉菜单的所有选项都变为空白。
在控制台中,对象 clonedOptions 上出现脚本错误 Invalid Calling Object error。
function dynamicSelect(id1, id2) {
//alert("Everytime")
// Browser and feature tests to see if there is enough W3C DOM support
// Obtain references to both select boxes
var sel1 = document.getElementById(id1);
var sel2 = document.getElementById(id2);
// Clone the dynamic select box
var clone = sel2.cloneNode(true);
// Obtain references to all cloned options
var clonedOptions = clone.getElementsByTagName("option");
// Onload init: call a generic function to display the related options in the dynamic select box
refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
// Onchange of the main select box: call a generic function to display the related options in the dynamic select box
sel1.onchange = function() {
refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
};
}
function refreshDynamicSelectOptions(sel1, sel2, clonedOptions) {
while (sel2.options.length) {
sel2.remove(0);
}
//alert(sel1.options[sel1.selectedIndex].value)
// Create regular expression objects for "select" and the value of the selected option of the main select box as class names
var pattern1 = /( |^)(select)( |$)/;
var pattern2 = new RegExp("( |^)(" + sel1.options[sel1.selectedIndex].value + ")( |$)");
// Iterate through all cloned options
//alert(clonedOptions.length);
for (var i = 0; i < clonedOptions.length; i++) {
// If the classname of a cloned option either equals "select" or equals the value of the selected option of the main select box
if (clonedOptions[i].className.match(pattern1) || clonedOptions[i].className.match(pattern2)) {
// Clone the option from the hidden option pool and append it to the dynamic select box
//alert("Did match")
sel2.appendChild(clonedOptions[i].cloneNode(true));
//alert(sel2.options[1]);
}
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>dynamic selectbox example</title>
<script type="text/javascript" src="unobtrusivedynamicselect_ex5.js"></script>
</head>
<body>
<form action="#">
<div>
<select id="pda-brand">
<option value="select">Select PDA brand...</option>
<option value="dell">Dell</option>
<option value="hp">HP</option>
<option value="palmone">PalmOne</option>
</select>
<select id="pda-type">
<option class="select" value="select">Select PDA type...</option>
<option class="dell" value="aximx30">Axim X30</option>
<option class="dell" value="aximx50">Axim X50</option>
<option class="hp" value="ipaqhx2750">iPAQ hx2750</option>
<option class="hp" value="ipaqrx3715">iPAQ rx3715</option>
<option class="hp" value="ipaqrz1710">iPAQ rz1710</option>
<option class="palmone" value="tungstene2">Tungsten E2</option>
<option class="palmone" value="tungstent5">Tungsten T5</option>
<option class="palmone" value="zire72">Zire 72</option>
</select>
<script type="text/javascript">
window.onload = function(e) {
dynamicSelect("pda-brand", "pda-type");
}
</script>
</div>
</form>
</body>
</html>
问题行
在下面的位置,对于 clonedOptions,我收到 Invalid Calling Object 错误。
for (var i = 0; i < clonedOptions.length; i++) {
【问题讨论】:
-
我猜这是因为
clonedOptions中的一些options不再存在,而您每次都使用clonedOptions的原始版本 -
@Haketo,它适用于除 IE11 以外的所有浏览器
-
是的,我阅读了代码,它应该可以工作,但我们正在谈论 IE ^^,抛出错误的行是什么?
-
@Haketo,查看我更新的问题
-
@Haketo 在 IE 更改列表指南中,我在 google 某处遇到说 IE 11 不再支持全局变量。
标签: javascript html internet-explorer cross-browser internet-explorer-11