简体   繁体   English

未被捕获的TypeError:将innerhtml设置为数组中的字符串时,无法设置null的属性“ innerHTML”

[英]Uncaught TypeError: Cannot set property 'innerHTML' of null- when setting the innerhtml to a string from an array

In the following code I am trying to set the cat names in a way that doesn't hardcode them to the html. 在下面的代码中,我试图以不将其硬编码到html的方式来设置猫的名称。 Because of that I am using an array. 因此,我正在使用数组。 however whenever I try to set the innerHTML properties to catNames[0] or catNames[1] I get an error. 但是,每当我尝试将innerHTML属性设置为catNames [0]或catNames [1]时,都会出现错误。 I don't know why it doesn't evaluate that array and find the string and put that on the page. 我不知道为什么它不评估该数组并找到字符串并将其放在页面上。

HTML HTML

<html>
<head>
<title>Cat Clicker</title>
</head>


<body>

    <div>
    <!-- name -->
    <h1 id="creatname1">y</h1>

    <!-- clickable image -->
    <img id="cat" src="img/cat.jpg">

    <!-- text -->
    <p> You have clicked
        <strong id="counter1">0</strong> times
    </p>
    </div>


<div>
    <!-- name -->
    <h1 id="createname2">x</h1>

    <!-- clickable image -->
    <img id="cat2" src="img/cat2.jpg">

    <!-- text -->
    <p> You have clicked <strong id="counter2">0</strong> times</p>

</div>

<script src="js/app.js"></script>
</body>

Javascript 使用Javascript

                       (function(){
   var image = document.getElementById('cat'), image2=     
    document.getElementById('cat2'),
    clicks1= document.getElementById('counter1'),clicks2=                         
    document.getElementById('counter2'),
    counterNumber1=0, counterNumber2=0, catNames=["Javi","Esteban"], name1=    
    document.getElementById('createname1'), name2=  
    document.getElementById('createname2');

name1.innerHTML= catNames[0];
name2.innerHTML= catNames[1];

function imageClickHandler() {
    counterNumber1++;
    clicks1.innerHTML= counterNumber1;
}

function imageClickHandler2() {
    counterNumber2++;
    clicks2.innerHTML= counterNumber2;
}



image.addEventListener('click',imageClickHandler,false);
image2.addEventListener('click',imageClickHandler2,false);

})();

I have changed a bit your code (actually, I intended the lines and make single variable declarations to be more readable. 我对您的代码进行了一些更改(实际上,我打算使行变得更清晰易懂。

The error was in the id you were using. 错误出在您正在使用的id中。 Specifically, in your HTML code you define the id creatname1 and you try to select the element with id createname1 . 具体来说,在HTML代码中,定义id creatname1然后尝试选择ID为createname1的元素。

 (function(){ var image = document.getElementById('cat'); var image2 = document.getElementById('cat2'); var clicks1 = document.getElementById('counter1'); var clicks2 = document.getElementById('counter2'); var counterNumber1=0; var counterNumber2=0; var catNames=["Javi","Esteban"]; var name1 = document.getElementById('createname1'); var name2 = document.getElementById('createname2'); name1.innerHTML= catNames[0]; name2.innerHTML= catNames[1]; function imageClickHandler() { counterNumber1++; clicks1.innerHTML= counterNumber1; } function imageClickHandler2() { counterNumber2++; clicks2.innerHTML= counterNumber2; } image.addEventListener('click',imageClickHandler,false); image2.addEventListener('click',imageClickHandler2,false); })(); 
  <div> <!-- name --> <h1 id="createname1">y</h1> <!-- clickable image --> <img id="cat" src="img/cat.jpg"> <!-- text --> <p> You have clicked <strong id="counter1">0</strong> times </p> </div> <div> <!-- name --> <h1 id="createname2">x</h1> <!-- clickable image --> <img id="cat2" src="img/cat2.jpg"> <!-- text --> <p> You have clicked <strong id="counter2">0</strong> times</p> </div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的类型错误:为隐藏字段设置 innerhtml 时,无法将属性“innerHTML”设置为 null - Uncaught TypeError: Cannot set property 'innerHTML' of null when setting innerhtml for hiddenfield 未捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null 未捕获的Typeerror:无法将属性innerHTML设置为null - Uncaught Typeerror: Cannot set property innerHTML of null 未捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null 未捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null 未捕获的TypeError:无法设置null的属性&#39;innerHTML&#39; - Uncaught TypeError: Cannot set property 'innerHTML' of null 错误:未捕获类型错误:无法设置 null 的属性(设置“innerHTML”) - Error: Uncaught TypeError: Cannot set properties of null (setting 'innerHTML') 未捕获(承诺中)类型错误:无法设置 null 的属性(设置“innerHTML”) - Uncaught (in promise) TypeError: Cannot set properties of null (setting 'innerHTML') Javascript 未捕获类型错误:无法设置 null 的属性(设置“innerHTML”) - Javascript Uncaught TypeError: Cannot set properties of null (setting 'innerHTML') 未捕获的TypeError:无法在phonegap中将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null in phonegap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM