ReferenceError:未在javascript

时间:2019-05-29 15:55:32

标签: javascript html

我正在从一门课程中练习JavaScript,我的代码与课程教程完全相同,但是它有错误并且无法正常工作。

我尝试了两种方式:

const ul = document.querySelector('.players');
const ul = window.document.querySelector('.players');

但仍然存在相同的问题。

<!Doctype html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width" , initial-scale=1, shrink>
    <meta name="theme-color" content="#000000">

    <title>ES6</title>
</head>


<body>

    <ul class="players"></ul>

</body>

<script src="nba.js"></script>

</html>

    const ul = document.querySelector('.players');


const players = [{
      jersey: 56,
      name: "Djounte Murray",
      position: "Point guard",
      PPG: 2.6
   },
   {
      jersey: 98,
      name: "Dennis Rodman",
      position: "Small Forward",
      PPG: 10.8
   },
   {
      jersey: 1,
      name: "Michael Jordan",
      position: "Small Forward",
      PPG: 345.6
   },
   {
      jersey: 2,
      name: "Lebron James",
      position: "Shooting Guard",
      PPG: 26.7
   },
   {
      jersey: 33,
      name: "Patrick Ewing",
      position: "Center",
      PPG: 20.2
   }
];

let list = '';
for (let i = 0; i < players.length; i++) {
   let player = palyers[i];

   list += `<li>${player.jersey} - ${palyer.name} --- ${palyer.position} --- ${player.PPG}</li>`
}

ul.insertAdjacentHTML("beforeend", list);

错误是:

const ul = document.querySelector('.players');
       ^

预期结果是将所有玩家信息打印为列表,如下所示:

56,name:"Djounte Murray"- position:"Point guard"---PPG:2.6
98,name:"Dennis Rodman" - position:"Small Forward"---PPG:10.8
1,name:"Michael Jordan" - position:"Small Forward"---PPG:345.6
2,name:"Lebron James"- position:"Shooting Guard"---PPG:26.7
33,name:"Patrick Ewing"- position:"Center"---PPG:20.2

0 个答案:

没有答案