<tfoot id='InT40'></tfoot>

    • <bdo id='InT40'></bdo><ul id='InT40'></ul>

    1. <legend id='InT40'><style id='InT40'><dir id='InT40'><q id='InT40'></q></dir></style></legend>

      <small id='InT40'></small><noframes id='InT40'>

      <i id='InT40'><tr id='InT40'><dt id='InT40'><q id='InT40'><span id='InT40'><b id='InT40'><form id='InT40'><ins id='InT40'></ins><ul id='InT40'></ul><sub id='InT40'></sub></form><legend id='InT40'></legend><bdo id='InT40'><pre id='InT40'><center id='InT40'></center></pre></bdo></b><th id='InT40'></th></span></q></dt></tr></i><div id='InT40'><tfoot id='InT40'></tfoot><dl id='InT40'><fieldset id='InT40'></fieldset></dl></div>

        如何修复“未捕获的类型错误:无法调用 null 的方法‘appendChild’"

        How to fix quot;Uncaught TypeError: Cannot call method #39;appendChild#39; of nullquot;(如何修复“未捕获的类型错误:无法调用 null 的方法‘appendChild’)

            <bdo id='ZdBfd'></bdo><ul id='ZdBfd'></ul>

                <tbody id='ZdBfd'></tbody>

              • <i id='ZdBfd'><tr id='ZdBfd'><dt id='ZdBfd'><q id='ZdBfd'><span id='ZdBfd'><b id='ZdBfd'><form id='ZdBfd'><ins id='ZdBfd'></ins><ul id='ZdBfd'></ul><sub id='ZdBfd'></sub></form><legend id='ZdBfd'></legend><bdo id='ZdBfd'><pre id='ZdBfd'><center id='ZdBfd'></center></pre></bdo></b><th id='ZdBfd'></th></span></q></dt></tr></i><div id='ZdBfd'><tfoot id='ZdBfd'></tfoot><dl id='ZdBfd'><fieldset id='ZdBfd'></fieldset></dl></div>
              • <legend id='ZdBfd'><style id='ZdBfd'><dir id='ZdBfd'><q id='ZdBfd'></q></dir></style></legend><tfoot id='ZdBfd'></tfoot>

                  <small id='ZdBfd'></small><noframes id='ZdBfd'>

                  本文介绍了如何修复“未捕获的类型错误:无法调用 null 的方法‘appendChild’"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试从 HTML 文本区域获取文本,并在按下提交"按钮时调用 create() 方法.该方法试图使用来自文本区域的消息,并将其发布到它自己的带有类的 p 标签,并在它自己的 p 标签和它自己的类中发布一个日期戳.

                  I am attempting to grab text from a HTML text area, and call the create() method when a 'Submit' button is pressed. The method is trying to use the message from the text area, and post that to its own p tag with class, and post a date stamp in its own p tag, and its own class.

                  这些都将在 div 的评论"中.我得到的错误(在 Chrome 中使用开发者工具)是

                  These will both be in the div 'comments'. The error I am getting (using developer tools in Chrome), is

                  未捕获的类型错误:无法调用 null 的方法appendChild".

                  Uncaught TypeError: Cannot call method 'appendChild' of null.

                  这是针对cmt.appendChild(divTag);"的.我对 Javascript 很陌生,这只是我提高技能的练习.非常感谢所有帮助!

                  This is aimed at "cmt.appendChild(divTag);". I am very new to Javascript, and this is just practise for me to increase my skills. All help is greatly appreciated!

                  var cmt = document.getElementById('comments');
                  
                  function create() {
                  
                      var username = 'User',
                          message = document.getElementById("textBox").value,
                          divTag = document.createElement('div'),
                          p1 = document.createElement('p'),
                          p2 = document.createElement('p');
                  
                      divTag.className = 'comment';
                  
                      p1.className = 'date';
                      p1.innerHTML = new Date();
                      divTag.appendChild(p1);
                  
                      p2.className = 'message';
                      p2.innerHTML = username + ': ' +message;
                      divTag.appendChild(p2);
                  
                      cmt.appendChild(divTag);
                  }
                  

                  推荐答案

                  您收到的错误表明您的页面上没有 ID 为comments"的元素.document.getElementById 将返回null 当没有找到具有该 ID 的元素时,因此 cmd.appendChild(divTag) 将作为 null.appendChild(divTag) 执行.

                  The error you are getting suggests that there is no element with the ID "comments" on your page. document.getElementById will return null when no element with such an ID is found, and thus cmd.appendChild(divTag) will be executed as null.appendChild(divTag).

                  如果您确定该元素存在,您可能正在执行分配 cmt 变量的 JavaScript,在浏览器创建该元素之前.为防止这种情况发生,标准做法是将包含您的外部 JavaScript <script> 标记放在结束 </body> 标记.

                  If you are certain that the element exists, you may be executing your JavaScript that assigns the cmt variable before that element is created by the browser. To prevent that, standard practice is to place the <script> tag which includes your external JavaScript just before the closing </body> tag.

                  如果由于某种原因无法移动脚本标记,请尝试运行使用 $(document).ready() (jQuery) 或等效.

                  If you can't move your script tag for some reason, try running the code that assigns the variable with $(document).ready() (jQuery) or equivalent.

                  这篇关于如何修复“未捕获的类型错误:无法调用 null 的方法‘appendChild’"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
                  <legend id='nQKE4'><style id='nQKE4'><dir id='nQKE4'><q id='nQKE4'></q></dir></style></legend>

                      <tbody id='nQKE4'></tbody>
                    • <bdo id='nQKE4'></bdo><ul id='nQKE4'></ul>

                        <i id='nQKE4'><tr id='nQKE4'><dt id='nQKE4'><q id='nQKE4'><span id='nQKE4'><b id='nQKE4'><form id='nQKE4'><ins id='nQKE4'></ins><ul id='nQKE4'></ul><sub id='nQKE4'></sub></form><legend id='nQKE4'></legend><bdo id='nQKE4'><pre id='nQKE4'><center id='nQKE4'></center></pre></bdo></b><th id='nQKE4'></th></span></q></dt></tr></i><div id='nQKE4'><tfoot id='nQKE4'></tfoot><dl id='nQKE4'><fieldset id='nQKE4'></fieldset></dl></div>
                          <tfoot id='nQKE4'></tfoot>
                          • <small id='nQKE4'></small><noframes id='nQKE4'>