简体   繁体   English

无法读取 null 错误的属性“innerHTML”。 我的脚本在正文的末尾,为什么我仍然收到此错误?

[英]Cannot read property 'innerHTML' of null error. My script is at the end of the body, why am I still getting this error?

My script is at the end of the body, which has been the basic answer I've seen given when searching for a solution.我的脚本在正文的末尾,这是我在寻找解决方案时看到的基本答案。 What am I doing wrong?我究竟做错了什么? Do I need to use async/await?我需要使用异步/等待吗? I've written almost this exact code before, and its worked.我以前写过几乎这个确切的代码,并且它起作用了。

HTML: HTML:

    <!DOCTYPE html>
<html>
<head>
    <title>Index</title>
    <link rel="stylesheet" type="text/css" href="styles.css">

</head>
<body id="body">
    <h1 id="header">Pokemon</h1>
    <div id="main-container">

    </div>
    <button id="button">Click</button>
    <script type="text/javascript" src="ajax.js"></script>
</body>
</html>

JS: JS:

let pokemon = document.querySelectorAll('.poke');
let button = document.getElementById('button');
let container = document.getElementById('main-container');


function loadPokemon() {
    const urs = 'https://pokeapi.co/api/v2/pokemon';
    let xhr = new XMLHttpRequest();
    xhr.onload = function() {
        if(xhr.status === 200) {
            const poke = JSON.parse(xhr.responseText)
            container.innerHTML+= `
            ${poke.results.map(function(pok, index) {
                return `
                <div class='poke'>
                    <img src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${(index + 1).toString()}.png">
                    </img>
                    ${pok.name}
                </div>
                `
            })}
            `
        }
    }
    xhr.open('GET', urs, true);
    xhr.send();
};


button.addEventListener('click', loadPokemon);

It is working here.. I did not make any changes to your code它在这里工作..我没有对您的代码进行任何更改

 let pokemon = document.querySelectorAll('.poke'); let button = document.getElementById('button'); let container = document.getElementById('main-container'); function loadPokemon() { const urs = 'https://pokeapi.co/api/v2/pokemon'; let xhr = new XMLHttpRequest(); xhr.onload = function() { if(xhr.status === 200) { const poke = JSON.parse(xhr.responseText) container.innerHTML+= ` ${poke.results.map(function(pok, index) { return ` <div class='poke'> <img src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${(index + 1).toString()}.png"> </img> ${pok.name} </div> ` })} ` } } xhr.open('GET', urs, true); xhr.send(); }; button.addEventListener('click', loadPokemon);
 <!DOCTYPE html> <html> <head> <title>Index</title> <link rel="stylesheet" type="text/css" href="styles.css"> </head> <body id="body"> <h1 id="header">Pokemon</h1> <div id="main-container"> </div> <button id="button">Click</button> <script type="text/javascript" src="ajax.js"></script> </body> </html>

Your code is working fine.您的代码运行良好。 Are you sure you copy and pasted the exact code?您确定复制并粘贴了确切的代码吗?

暂无
暂无

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

相关问题 无法将属性“ innerHTML”设置为null,为什么会出现此错误 - Cannot set property 'innerHTML' of null why am I getting this error 为什么会出现“无法读取null的属性样式”错误? - Why I am getting “cannot read property style of null” error? 为什么我的脚本中出现“未捕获的TypeError:无法读取null的属性&#39;nodeName&#39;”? - Why am I getting “Uncaught TypeError: Cannot read property 'nodeName' of null” in my script? 我为什么会收到“无法在ahint设置null的属性&#39;innerHTML&#39;的信息” - Why am I getting “Cannot set property 'innerHTML' of null at ahint” 为什么我使用Knockout JS获得“无法读取属性'nodeType'的null”错误? - Why am I getting a “Cannot read property 'nodeType' of null” error with Knockout JS? 使用解析的字符串时,为什么会出现错误“无法读取null的属性&#39;名称&#39;”? - Why am I getting error 'Cannot read property 'name' of null' when using a parsed string? 为什么我会收到 TypeError: Cannot read property '0' of undefined error in google apps script? - Why am I getting TypeError: Cannot read property '0' of undefined error in google apps script? 为什么我越来越无法读取未定义错误的属性? - why I am getting, cannot read property of undefined error? “无法读取 null 的属性‘innerHTML’”错误 - "cannot read property 'innerHTML' of null" error 尝试通过电子邮件进行身份验证时,出现“ Uncaught TypeError:无法读取null的属性&#39;value&#39;”错误。 为什么? - When attempting to authenticate with Email, I get an “Uncaught TypeError: Cannot read property 'value' of null” Error. Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM