开发者问题收集

未捕获的类型错误:无法设置 null 属性(设置‘src’)

2021-11-27
29531

我在制作计数器网页时遇到问题,代码看起来不错,但网页在控制台中显示此错误。 未捕获的 TypeError:无法设置 null 属性(设置“src”)

请点击此链接查看我的错误

这是我的 javascript 代码和 Html 代码。

javascript:

// local reviews data

const reviews = [ { id: 1, name: "susan smith", job: "web developer", img: "https://res.cloudinary.com/diqqf3eq2/image/upload/v1586883334/person-1_rfzshl.jpg", text: "I'm baby meggings twee health goth +1. Bicycle rights tumeric chartreuse before they sold out chambray pop-up. Shaman humblebrag pickled coloring book salvia hoodie, cold-pressed four dollar toast everyday carry", }, { id: 2, name: "anna johnson", job: "web designer", img: "https://res.cloudinary.com/diqqf3eq2/image/upload/v1586883409/person-2_np9x5l.jpg", text: "Helvetica artisan kinfolk thundercats lumbersexual blue bottle. Disrupt glossier gastropub deep v vice franzen hell of brooklyn twee enamel pin fashion axe.photo booth jean shorts artisan narwhal.", }, { id: 3, name: "peter jones", job: "intern", img: "https://res.cloudinary.com/diqqf3eq2/image/upload/v1586883417/person-3_ipa0mj.jpg", text: "Sriracha literally flexitarian irony, vape marfa unicorn. Glossier tattooed 8-bit, fixie waistcoat offal activated charcoal slow-carb marfa hell of pabst raclette post-ironic jianbing swag.", }, { id: 4, name: "bill anderson", job: "the boss", img: "https://res.cloudinary.com/diqqf3eq2/image/upload/v1586883423/person-4_t9nxjt.jpg", text: "Edison bulb put a bird on it humblebrag, marfa pok pok heirloom fashion axe cray stumptown venmo actually seitan. VHS farm-to-table schlitz, edison bulb pop-up 3 wolf moon tote bag street art shabby chic. ", }, ];

const img = document.getElementById("img-container");
const author = document.getElementById("author");
const job = document.getElementById("job");
const info = document.getElementById("info");

const prevBtn = document.querySelector(".prev-btn");
const nextBtn = document.querySelector(".next-btn");
const randomBtn = document.querySelector(".random-btn");

// set current item
let currentItem = 0;

// load initial Item

window.addEventListener("DOMContentLoaded", ()=> {
const item = reviews[currentItem]
img.src = item.img;
});

HTML 代码:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta http-equiv="X-UA-Compatible" content="IE=edge">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Reviews</title>
 <link rel="stylesheet" href="./fontawesome-free-5.12.0-web/css/all.min.css">
 <link rel="stylesheet" href="styles.css">
</head>
<body>
 <main>
  <section class="container">
   <div class="title">
    <h2>our Reviews</h2>
    <div class="underline"></div>

   </div>
   <article class="review">
    <div class="img-container">
     <img src="./person-1.jpeg" id="person-img" alt="person image">
    </div>
    <h4 id="author">sara jones</h4>
    <p id="job">ux designer</p>
    <p id="info">
     Lorem, ipsum dolor sit amet consectetur adipisicing elit. Unde vitae eius facilis natus aliquid accusantium cum distinctio cupiditate animi numquam?
    </p>
    <div class="button-container">
     <button class="prev-btn">
      <i class="fas fa-chevron-left"></i>
     </button>
      <button class="next-btn">
      <i class="fas fa-chevron-right"></i>
     </button>
    </div>
     <button class="random-btn">suprise me</button>
    

   </article>
  </section>
 </main>
 <script src="app.js"></script>
</body>
</html>
2个回答

首先,正如aerial301所说,您不是定位到 img 标签,而是定位到 div 标签。

其次,您应该将html从 <div class="img-container"> 更改为 <div id="img-container"> ,您定位到的是带有 class img-container 的div,而不是 id

const reviews = [ { id: 1, name: "susan smith", job: "web developer", img: "https://res.cloudinary.com/diqqf3eq2/image/upload/v1586883334/person-1_rfzshl.jpg", text: "I'm baby meggings twee health goth +1. Bicycle rights tumeric chartreuse before they sold out chambray pop-up. Shaman humblebrag pickled coloring book salvia hoodie, cold-pressed four dollar toast everyday carry", }, { id: 2, name: "anna johnson", job: "web designer", img: "https://res.cloudinary.com/diqqf3eq2/image/upload/v1586883409/person-2_np9x5l.jpg", text: "Helvetica artisan kinfolk thundercats lumbersexual blue bottle. Disrupt glossier gastropub deep v vice franzen hell of brooklyn twee enamel pin fashion axe.photo booth jean shorts artisan narwhal.", }, { id: 3, name: "peter jones", job: "intern", img: "https://res.cloudinary.com/diqqf3eq2/image/upload/v1586883417/person-3_ipa0mj.jpg", text: "Sriracha literally flexitarian irony, vape marfa unicorn. Glossier tattooed 8-bit, fixie waistcoat offal activated charcoal slow-carb marfa hell of pabst raclette post-ironic jianbing swag.", }, { id: 4, name: "bill anderson", job: "the boss", img: "https://res.cloudinary.com/diqqf3eq2/image/upload/v1586883423/person-4_t9nxjt.jpg", text: "Edison bulb put a bird on it humblebrag, marfa pok pok heirloom fashion axe cray stumptown venmo actually seitan. VHS farm-to-table schlitz, edison bulb pop-up 3 wolf moon tote bag street art shabby chic. ", }, ];

const img = document.getElementById("img-container");
const author = document.getElementById("author");
const job = document.getElementById("job");
const info = document.getElementById("info");

const prevBtn = document.querySelector(".prev-btn");
const nextBtn = document.querySelector(".next-btn");
const randomBtn = document.querySelector(".random-btn");

// set current item
let currentItem = 0;

// load initial Item

window.addEventListener("DOMContentLoaded", ()=> {
const item = reviews[currentItem]
img.src = item.img;
});
<main>
  <section class="container">
   <div class="title">
    <h2>our Reviews</h2>
    <div class="underline"></div>

   </div>
   <article class="review">
    <div id="img-container">
     <img src="./person-1.jpeg" id="person-img" alt="person image">
    </div>
    <h4 id="author">sara jones</h4>
    <p id="job">ux designer</p>
    <p id="info">
     Lorem, ipsum dolor sit amet consectetur adipisicing elit. Unde vitae eius facilis natus aliquid accusantium cum distinctio cupiditate animi numquam?
    </p>
    <div class="button-container">
     <button class="prev-btn">
      <i class="fas fa-chevron-left"></i>
     </button>
      <button class="next-btn">
      <i class="fas fa-chevron-right"></i>
     </button>
    </div>
     <button class="random-btn">suprise me</button>
    

   </article>
  </section>
 </main>
Alan Omar
2021-11-27

错误 无法设置 null 的属性 表示根对象为 null。在您的例子中,img 为 null。

您似乎想要做的是更改图像显示的图片,但是您的代码目前存在 2 个问题。

  1. 查看您的代码,没有 id 为 img-container 的元素。但是,有一个同名的类。两者之间的差异相当大,它们不能互换使用。

  2. 即使您设置了 id,而不是类,您选择的元素也是 div,而不是图像。您需要定位 id 为 person-img 的元素来修改图像。

如果您对类和 ID 之间的区别感到好奇,这里有一个小的细分。

类可以多次重复用于不同的元素,这使您可以将相同的样式应用于多个元素,并通过 JavaScript 获取页面上具有该类的所有对象的集合。但是,ID 应该是唯一的,可用于挑选特定元素。

您可以在 此处 了解更多信息。

nab138
2021-11-27