admin管理员组文章数量:1320814
I am getting a cloneNode error Uncaught TypeError: Cannot read property 'cloneNode' of null at application.js:42
when I move my script into a javascript file. But the application works when the script is in the HTML page.
const candidatesTable = document.getElementById("candidates_example");
const newCandidatesTable = candidatesTable.cloneNode(true);
document.body.appendChild(newCandidatesTable);
I cannot figure out why this is happening and I am using Chrome for the browser. /
I am getting a cloneNode error Uncaught TypeError: Cannot read property 'cloneNode' of null at application.js:42
when I move my script into a javascript file. But the application works when the script is in the HTML page.
const candidatesTable = document.getElementById("candidates_example");
const newCandidatesTable = candidatesTable.cloneNode(true);
document.body.appendChild(newCandidatesTable);
I cannot figure out why this is happening and I am using Chrome for the browser. https://jsfiddle/sithanga/sd5qtcj3/2/
Share Improve this question asked Dec 8, 2018 at 16:49 SSSSSS 331 gold badge1 silver badge3 bronze badges1 Answer
Reset to default 6You are using document.body but your script is inserted in header, before executing any code on elements you should make sure they are existing on the page. Why it works when you insert it as script tag is you are inserting it at bottom.
e.g DOMContentLoaded event will be fired when all DOM content is loaded and ready to be interacted by your JS code.
本文标签:
版权声明:本文标题:javascript - Cannot read property 'cloneNode' of null, but works when the script is on HTML - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742088942a2420150.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论