以下脚本会出现这样的错误:
uncaught exception: [Exception... "Could not convert JavaScript argument arg 0
[nsIDOMHTMLSelectElement.add]" nsresult:
"0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)" location:
"JS frame :: file:///D:/programming/temp/js_bug/page.html
:: addOption :: line 13" data: no]
一段带脚本的HTML:
<form name = "form">
<select name = "select">
</select>
</form>
<script>
//This way of adding an option fails
function addOption(selectElement, optionText)
{
var option = document.createElement("option")
option.text = optionText
selectElement.add(optionText, null);
}
element = document.getElementsByName("select")[0]
addOption(element, "2")
/* This way of adding an option works
option = document.createElement("option")
option.text = "1"
element.add(option, null)*/
</script>
评论代码正常运行。我只能看到有效的代码和失败的代码之间的区别 - 将DOM对象作为函数参数传递 我正在使用Firefox 3.6.17
答案 0 :(得分:0)
我认为您应该将option
(不是optionText
)传递给add()
方法。