【JavaScript】关于Uncaught TypeError: Cannot set properties of null (setting ‘innerHTML‘)报错

问题:

Uncaught TypeError: Cannot set properties of null (setting 'innerHTML')的意思是 未捕获的TypeError:不能设置null属性(设置'innerHTML') 。

错误代码:

输出内容:执行getElementById.innerHTML时找不到demo的内容,找不到你想将所写的HTML代码插入的地方。如图示:

 

分析原因:

此问题涉及到HTML中Javascript脚本的加载和执行,浏览器总是从上到下加载整个 HTML DOM。任何写在标签内的 js 代码都会被浏览器渲染引擎执行,甚至在加载整个body 标签内的各种 HTML 元素标签之前。当执行到innerHTML这一行代码时,并没有加载到下面的DOM结构,所以就会报错无法读取HTML。

DOM文档加载步骤: 

  1. 解析HTML结构 
  2. 加载外部的脚本和样式文件 
  3. 解析并执行脚本代码 
  4. 执行$(function(){})内对应代码 
  5. 加载图片等二进制资源 
  6. 页面加载完毕,执行window.onload

解决方法:

方法一:将js放在底部,等浏览器自上往下完全加载完DOM后,再执行js脚本就不会出现空引用。

代码示例:

 输出结果:

 方法二:使用事件挂钩 window.onload,在原本的script标签中添加window.onload,让浏览器加载完成DOM后再执行脚本。

代码示例:

 输出结果:

 参考链接:HTML中Javascript脚本的加载和执行_Cassie's blog-CSDN博客_html执行js代码

javascript - Cannot set property 'innerHTML' of null - Stack Overflow

  • 14
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Delphi 7.1 Update Release Notes=======================================================This file contains important supplemental and late-breakinginformation that may not appear in the main productdocumentation, and supersedes information contained in otherdocuments, including previously installed release notes.Borland recommends that you read this file in its entirety.NOTE: If you are updating a localized version of Delphi 7, visit the Borland Registered User web site to obtain a localized readme file that may contain important late- breaking information not included in this readme file.IMPORTANT: Delphi must be closed before installing this update. =====================================================CONTENTS * INSTALLING THIS UPDATE * UPDATING LOCALIZED VERSIONS OF DELPHI 7 * KNOWN ISSUES * ISSUES ADDRESSED BY THIS UPDATE - IDE - CORE DATABASE - DATASNAP - DBGO (ADO COMPONENTS) - dbExpress - dbExpress COMPONENTS AND DB VCL - dbExpress CORE DRIVER AND METADATA - dbExpress VENDOR ISSUES - dbExpress CERTIFICATION - WEB SNAP - ACTIVEX - COMPILER - RTL - VCL - THIRD PARTY - BOLD FOR DELPHI * VERIFYING THAT THE UPDATE WAS SUCCESSFUL * FILES INSTALLED BY THIS UPDATE =======================================================INSTALLING THIS UPDATE* This update can not be applied to Delphi 7 Architect Trial version. * This update can not be removed after it is installed.* You will need the original Delphi 7 installation CD available to install this update.* To install this update from the CD, insert the CD, and launch the d7_ent_upd1.exe file appropriate for your locale.* To install this update from the Web, double-click the self-executing installation file and follow the prompts. * The Delphi 7 documentation PDF files are available on the update CD.========================================================UPDATING LOCALIZED VERSIONS OF DELPHI 7* This update can be applied only to the English version of Delphi 7. There are separate updates for the German, French and Japanese ver

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值