简体   繁体   English

JavaScript RunTime错误:appendChild() - 对IE8中的方法或属性的意外调用

[英]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: 这可能是因为:

  1. Such an object doesn't exist in your page 您的页面中不存在此类对象
  2. You didn't get the id exactly right 你没有得到完全正确的id
  3. You're running this code before the page has loaded so the object hasn't yet been loaded/created. 您在页面加载之前运行此代码,因此尚未加载/创建对象。

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.

相关问题 IE8 appendChild“对方法或属性访问的意外调用” - IE8 appendChild “Unexpected call to method or property access” jQuery 1.11.1 appendChild错误IE8(意外调用方法或属性访问) - jQuery 1.11.1 appendChild error IE8 (Unexpected call to method or property access) JavaScript IE appendChild() - 对方法或属性访问的意外调用 - JavaScript IE appendChild() - Unexpected call to method or property access excanvas IE8,对方法或属性访问的意外调用 - excanvas IE8, Unexpected call to method or property access 在jQuery中对IE8上的方法或属性访问进行了意外调用 - Unexpected call to method or property access on IE8 in jQuery marionettejs attachhtml ie8-意外调用方法或属性 - marionettejs attachhtml ie8 - unexpected call to method or property Javascript IE错误:意外调用方法或属性访问 - Javascript IE error: unexpected call to method or property access 为什么此jQuery代码会导致IE8错误:“方法或属性访问意外调用” - Why does this jQuery code cause IE8 error: “UNEXPECTED CALL TO METHOD OR PROPERTY ACCESS” this.appendChild(a)-意外调用方法或属性 - this.appendChild(a) - Unexpected call to method or property access 对方法或属性访问的意外调用-appendChild() - Unexpected call to method or property access - appendChild()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM