【问题标题】:AppendChild() is not a function javascriptAppendChild() 不是一个函数 javascript
【发布时间】:2014-05-15 09:12:12
【问题描述】:

这是我的javascript

<head runat="server">
    <script type="text/javascript" language="javascript">

        function createQuestionPanel() {

            var element = document.createElement("Input");
            element.setAttribute("type", "button");
            element.setAttribute("value", "button");
            element.setAttribute("name", "button");


            var div = '<div>top div</div>';
            div[0].appendChild(element);

        }

        function formvalidate() {

        }

    </script>
</head>
<body onload="createQuestionPanel()">
</body>
</html>

它抛出错误 "AppendChild is not a function" 。我试图寻找解决方案..有人建议在

的地方

div.appendChild(元素);

这应该发布

div[0].appendChild(元素);

它没有改变错误。请推荐

【问题讨论】:

  • 该值是一个字符串,而不是一个 DOM 元素。字符串没有appendChild() 方法。查看您创建&lt;input&gt; 元素的方式,您将需要类似&lt;div&gt; 的内容。
  • 就像你创建 input 元素一样,创建一个这样的 div 元素让 appendChild 工作
  • 您应该将元素附加到元素。创建具有“top div” ID 的 div 元素和:document.getElementById("top div").appendChild(element);

标签: javascript html .net


【解决方案1】:

您的 div 变量是一个字符串,而不是 DOM 元素对象:

var div = '<div>top div</div>';

字符串没有appendChild 方法。与其创建原始 HTML 字符串,不如将 div 创建为 DOM 元素并附加一个文本节点,然后附加输入元素:

var div = document.createElement('div');
div.appendChild(document.createTextNode('top div'));

div.appendChild(element);

【讨论】:

    【解决方案2】:
     function createQuestionPanel() {
    
            var element = document.createElement("Input");
            element.setAttribute("type", "button");
            element.setAttribute("value", "button");
            element.setAttribute("name", "button");
    
    
            var div = document.createElement("div"); <------- Create DIv Node
            div.appendChild(element);<--------------------
            document.body.appendChild(div) <------------- Then append it to body
    
        }
    
        function formvalidate() {
    
        }
    

    【讨论】:

      【解决方案3】:

      在这

      var div = '<div>top div</div>';
      

      "div" 不是 DOM 对象,只是一个字符串,并且 string 没有 string.appendChild.

      以下是一些对 appendChild 方法可能有所帮助的参考资料:

      <div id="div1">
      <p id="p1">This is a paragraph.</p>
      <p id="p2">This is another paragraph.</p>
      </div>
      
      <script>
      var para = document.createElement("p");
      var node = document.createTextNode("This is new.");
      para.appendChild(node);
      
      var element = document.getElementById("div1");
      element.appendChild(para);
      </script>
      

      【讨论】:

        【解决方案4】:

        改变一下

        var div = '<div>top div</div>'; // you just created a text string
        

        到

        var div = document.createElement("div"); // we want a DIV element instead
        div.innerHTML = "top div";
        

        【讨论】:

          【解决方案5】:

          尝试以下方法:

          var div = document.createElement("div");
          div.innerHTML = "topdiv";
          div.appendChild(element);
          document.body.appendChild(div);
          

          【讨论】:

            【解决方案6】:

            我只使用$(".ClassName")[0].appendChild(div); 或$("#IdName")[0].appendChild(div); x)

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2017-12-20
              • 1970-01-01
              • 2017-10-20
              • 2022-12-05
              • 2021-11-05
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多