[英]JavaScript RunTime Error : appendChild() - Unexpected call to method or property in IE8
I am getting runtime JS error like : "Unexpected call to method or property". 我得到运行时JS错误,如:“意外调用方法或属性”。 Here is the code :
这是代码:
var comboInput = document.getElementById("Combo");
var option1 = document.createElement("option");
option1.value = "-1";
option1.innerHTML = "--Select--";
comboInput.appendChild(option1); //At this line debugger breaks and
// throws the above error
I gone through some of similar questions on SO about this issue but still not getting the solution. 关于这个问题,我在SO上遇到了一些类似的问题,但仍然没有得到解决方案。
Please help.. Thanks in adv... 请帮忙..谢谢你...
You need to check on the value of comboInput
(output its value with console.log()
or in an alert()
to see what it's set to). 您需要检查
comboInput
的值(使用console.log()
或alert()
输出其值以查看它的设置)。
It likely has a value of null
because your code didn't find an existing object with id="Combo"
. 它的值可能为
null
因为您的代码未找到id="Combo"
的现有对象。 This can be because: 这可能是因为:
If you include the entire page context (all HTML and all JS or a link to a live page), we could tell you more specifically what steps should be taken to correct the issue. 如果您包含整个页面上下文(所有HTML和所有JS或指向实际页面的链接),我们可以更具体地告诉您应采取哪些步骤来纠正问题。
You can't append children to <input>
elements. 您不能将子项附加到
<input>
元素。
<option>
elements can only be children of <select>
elements, where <select>
elements are html's version of a drop-down list (or a multiselect list depending on the attributes you give it). <option>
元素只能是<select>
元素的子元素,其中<select>
元素是html的下拉列表版本(或多选列表,具体取决于您提供的属性)。
Try changing your html so that your "Combo" element is like this: 尝试更改您的html,以便您的“Combo”元素如下所示:
<select id="Combo"></select>
and then your code should work. 然后你的代码应该工作。
Note though that you can set options directly in the html markup, eg, 请注意,您可以直接在html标记中设置选项,例如,
<select id="Combo">
<option value="-1">--Select--</option>
<option value="1">Green</option>
<option value="2">Purple</option>
</select>
(And having put options in your html you can add additional options and/or remove options via JS.) (并且在您的html中添加了选项,您可以添加其他选项和/或通过JS删除选项。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.