前些天在做项目的时候要使用JS给SELECT动态添加OPTION,遇到了个问题,JS代码如下:
while (obj.selectedIndex > -1){
mot = obj.options[obj.selectedIndex].text;
mov = obj.options[obj.selectedIndex].value;
obj.remove(obj.selectedIndex);
var newoption = document.createElement("OPTION");
newoption.text = mot;
newoption.value = mov;
target.add(newoption);
}
而Firefox却不认
target.add(newoption);
出错提示:
uncaught exception: [Exception... "Not enough arguments" nsresult: "0x80570001 (NS_ERROR_XPC_NOT_ENOUGH_ARGS)" location: "JS frame :: http://localhost/untitled.asp :: moveselect :: line 23" data: no]
修改代码如下:
while (obj.selectedIndex > -1){
mot = obj.options[obj.selectedIndex].text;
mov = obj.options[obj.selectedIndex].value;
obj.remove(obj.selectedIndex);
var newoption = document.createElement("OPTION");
newoption.text = mot;
newoption.value = mov;
if(Sys.ie) target.add(newoption);
if(Sys.firefox) target.appendChild(newoption);
}
谢谢各位!是鄙人寡闻了
本文解决了一个在使用JavaScript为SELECT元素动态添加OPTION时遇到的问题,在Firefox浏览器中使用target.add方法失败,通过判断浏览器类型并采用不同的方法成功实现了跨浏览器兼容。

1463

被折叠的 条评论
为什么被折叠?



