我有一个字符串数组" GlobalObject"用于将值加载到选择中,以下代码在Chrome中完美运行但在IE 11中不能。我不知道为什么。字符串的排序如下:
14000 // optgroup
14239
14238
14238
等
V8Instance *
它插入第一个optgroup,然后在IE11中插入任何内容。
答案 0 :(得分:1)
这是一个有效的例子。我希望你能适应你的情况。
<button onclick="myFunction()">Try it</button>
<select id="mySelect" size="6">
</select>
<script>
var arr=['1111'/*not optgroup*/,'0123'/*optgroup*/,'1234','2345','0567','5678','6789'];
function myFunction() {
var x = document.getElementById("mySelect");
var gr = null;//future optgroup
for(var i=0,n=arr.length;i<n;i++){
if(arr[i].indexOf('0')==0){//starts with 0 - optgroup
gr = document.createElement("OPTGROUP");
gr.label = arr[i];
x.appendChild(gr);
}
else {
var op = document.createElement("option");
op.textContent = arr[i];
op.value = arr[i];
if(gr)//optgroup exists
gr.appendChild(op);
else //optgroup not exists
x.appendChild(op);
}
}//for
}//myFunction
</script>
var arr=['1111','0123','1234','2345','0567','5678','6789'];
function myFunction() {
var x = document.getElementById("mySelect");
var gr = null;
for(var i=0,n=arr.length;i<n;i++){
if(arr[i].indexOf('0')==0){
gr = document.createElement("OPTGROUP");
gr.label = arr[i];
x.appendChild(gr);
}
else {
var op = document.createElement("option");
op.textContent = arr[i];
op.value = arr[i];
if(gr)
gr.appendChild(op);
else
x.appendChild(op);
}
}
}
&#13;
<button onclick="myFunction()">Try it</button>
<select id="mySelect" size="6">
</select>
&#13;
答案 1 :(得分:0)
使用以下代码修复了问题:
for(var i = 1; i < GlobalObject[0].length; i++) {
var opt = GlobalObject[0][i];
var subString = opt.substring(opt.length - 4, opt.length);
if (i != GlobalObject[0].length -1 ) {
var opt2 = GlobalObject[0][i+1];
var subString2 = opt2.substring(opt2.length - 4, opt2.length);
}
else {}
if (subString != "0000") {
var el = document.createElement("option");
el.textContent = "\u00A0" + "\u00A0" + "\u00A0" + "\u00A0" + "\u00A0" + opt;
el.value = opt;
newOptGroup.appendChild(el);
}
else {
var newOptGroup = document.createElement("OPTGROUP");
newOptGroup.label = opt;
newOptGroup.value = opt;
}
if (subString2 == "0000" || i == GlobalObject[0].length-1 ) {
console.log("here");
ChannelList.appendChild(newOptGroup);
}
}