空&QUOT不能调用方法'的appendChild';:未捕获类型错误,如何修复&QUOT [英] How to fix "Uncaught TypeError: Cannot call method 'appendChild' of null"
问题描述
我试图抓住从HTML文本区域的文本,并调用Create()方法时,提交按钮pressed。该方法尝试使用的消息从文本区,并张贴到带班自身的p标签,并在其自己的p标签,以及它自己的类发布日期戳。
这些都将成为在格评论。我得到(使用Chrome开发者工具),误差
遗漏的类型错误:不能调用方法'的appendChild'空的
块引用>这是针对cmt.appendChild(divTag);。我非常新的JavaScript,而这仅仅是练习,我提高我的技能。所有帮助是极大的AP preciated!
VAR CMT =的document.getElementById('意见');函数来创建(){ VAR用户名='用户',
消息=的document.getElementById(textBox中)。价值,
divTag =使用document.createElement('DIV'),
P1 =使用document.createElement('P'),
P2 =使用document.createElement('P'); divTag.className ='评论'; p1.className ='日期';
p1.innerHTML =新的日期();
divTag.appendChild(P1); p2.className =信息;
p2.innerHTML =用户名+:+消息;
divTag.appendChild(P2); cmt.appendChild(divTag);
}
解决方案你所得到的错误表明,有你的页面上的ID意见没有元素。
的document.getElementById
将返回空
当这样的ID没有元素被发现,从而cmd.appendChild(divTag)
会null.appendChild执行(divTag)
。如果您确定该元素存在,你可能会执行你的JavaScript分配
CMT
变量之前是由浏览器创建的元素。以prevent的是,标准的做法是将<脚本>
标签,其中包括你的外部JavaScript的刚刚结束&LT前; / BODY方式>
标记如果您不能移动你的脚本标签出于某种原因,尝试运行code,变量与
的$(document)分配。就绪()
( jQuery的)<一个href=\"http://stackoverflow.com/questions/3989095/javascripthow-to-write-document-ready-like-event-without-jquery\">or相当于。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.
These will both be in the div 'comments'. The error I am getting (using developer tools in Chrome), is
Uncaught TypeError: Cannot call method 'appendChild' of null.
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); }
解决方案The error you are getting suggests that there is no element with the ID "comments" on your page.
document.getElementById
will returnnull
when no element with such an ID is found, and thuscmd.appendChild(divTag)
will be executed asnull.appendChild(divTag)
.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.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.这篇关于空&QUOT不能调用方法'的appendChild';:未捕获类型错误,如何修复&QUOT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!