javascript 无法读取 null 的属性“innerHTML”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13909567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:08:53  来源:igfitidea点击:

Cannot read property 'innerHTML' of null

javascriptasp.nethtml

提问by Mr_Green

I am beginner in javascript.

我是 javascript 初学者。

js file

js文件

         var msgErrorPalletizedWeight = document.getElementById('msgError');
         function palletizedWeightValidation() {
              console.log("hello");
/*ERROR*/     msgErrorPalletizedWeight.innerHTML += ("Please enter all the values marked with *");
         }

HTML

HTML

<head runat="server">
   <title></title>
   <link href="Styles/Site.css" rel="stylesheet" type="text/css" />
   <script src="Scripts/file.js" type="text/javascript"></script>
</head>
<body>
    <p id="msgError"></p>
    <asp:Button ID="btnSubmitPalletized" runat="server" Text="Submit" 
                   OnClientClick="palletizedWeightValidation()" />
</body>

ERROR

错误

Cannot read property 'innerHTML' of null

无法读取 null 的属性“innerHTML”

I searched many sites before asking here, they all recommending to check for null before validations which I can do but my problem here is I want to add some text to the ptag as you can see.. How to achieve that? I even tried appending, thought it might solve the problem. I know this is a silly mistake because there are many posts on this... the thing is that I cant understand where I am going wrong.

我在问这里之前搜索了很多网站,他们都建议在验证之前检查空值,我可以这样做,但我的问题是我想在p标签中添加一些文本,如您所见..如何实现?我什至尝试附加,认为它可以解决问题。我知道这是一个愚蠢的错误,因为有很多关于这个的帖子......问题是我无法理解我哪里出错了。

Please help.

请帮忙。

回答by Adil

You are tryingto access the elementof html when it is not ready, put the statement in event function to get the element in function palletizedWeightValidation.

trying要访问的elementhtml 当它不是时ready,将语句放在事件函数中以获取函数中的元素palletizedWeightValidation

      function palletizedWeightValidation() {
              var msgErrorPalletizedWeight = document.getElementById('msgError');
              console.log("hello");
/*ERROR*/     msgErrorPalletizedWeight.innerHTML += ("Please enter all the values marked ith *");
         }

EDITTo stop postback, you need to return false from javascrip function.

编辑要停止回发,您需要从 javascript 函数返回 false。

 <asp:Button ID="btnSubmitPalletized" runat="server" Text="Submit" 
                   OnClientClick="return palletizedWeightValidation()" />


      function palletizedWeightValidation() {
              var msgErrorPalletizedWeight = document.getElementById('msgError');
              console.log("hello");
/*ERROR*/     msgErrorPalletizedWeight.innerHTML += ("Please enter all the values marked ith *");
              return false; // returning true will cause postback.
         }

回答by Luka

The important thing is when your js file gets executed. If you load js file inside head tag, your file will be executed before DOM is created. That means, document.getElementById function returns null.

重要的是你的 js 文件何时被执行。如果你在 head 标签中加载 js 文件,你的文件将在创建 DOM 之前执行。这意味着,document.getElementById 函数返回 null。

回答by Justin McDonald

The error you are getting is caused because you are trying to read an attribute of a NULL object. This means that your line:

你得到的错误是因为你试图读取一个 NULL 对象的属性。这意味着您的线路:

var msgErrorPalletizedWeight = document.getElementById('msgError');

is not finding any elements on your page.

没有在您的页面上找到任何元素。