简体   繁体   English

尝试在 javascript 中更改 src 时出现空图像

[英]Empty image when trying to change src in javascript

I am trying to change an image and some text in javascript trough a button.I can change the text but no matter what I do if I change the image it's just an image not found icon我正在尝试通过一个按钮更改 javascript 中的图像和一些文本。我可以更改文本,但无论我做什么,如果我更改图像,它只是一个图像未找到图标

Here's my relevant html: `这是我的相关 html:`

<div class="review1">
      <img id="student" src="images\review1.png" />
      <div id="review1text">
        <h1 id="reviewnume">Alina,21</h1>
        <p id="reviewtext">
          Sunt studenta la Politehnica in Bucuresti si Pitagora.ro m-a ajutat
          foarte mult sa aprofundez cunostiintele mele de algebra.Recomand cu
          cea mai mare incredere!
        </p>
      </div>

     <i class="fa-solid fa-circle-arrow-left"></i>
      <i class="fa-solid fa-circle-arrow-right"></i>
      
    </div>

And here is my javascript:

let btnNext = document.querySelector("fa-solid fa-circle-arrow-right");
let btnprevious = document.querySelector("fa-solid.fa-circle-arrow-left");



btnNext = addEventListener("click", () => {
  document.getElementById("reviewtext").textContent =
    "Nu am reusit sa ajung la cursurile mele de algebra din cause serviciului.In orice caz,nu a fost mare pierdere deoarece Pitagora.ro m-a ajutat sa recuperez materia fara probleme";
  document.getElementById("reviewnume").textContent = "Bogdan,22";
  document.getElementById("reviewnume").style.textAlign = "center";
  document.getElementById("reviewtext").style.fontSize = "25px";
  document.getElementById("reviewtext").style.marginLeft = "20px";
  document.getElementById("student").src = "images\review2.png";
});

Again,the text on "reviewnume" and "reviewtext" changes when I click but the image from "review1" to "review2" doesn't,even though they are in the same folder同样,当我单击时,“reviewnume”和“reviewtext”上的文本会发生变化,但从“review1”到“review2”的图像不会,即使它们在同一个文件夹中

You are getting this error because you forgot to use the dot(.) operator when finding an element using a class name.您收到此错误是因为您在使用类名查找元素时忘记使用dot(.)运算符。

I mean, you did-我的意思是,你确实-

let btnNext = document.querySelector("fa-solid fa-circle-arrow-right");

But you should do-但你应该做——

let btnNext = document.querySelector(".fa-solid .fa-circle-arrow-right");
or
let btnNext = document.querySelector(".fa-circle-arrow-right");

You also used the wrong syntax for the event listener.您还为事件侦听器使用了错误的语法。

btnNext = addEventListener("click", () => {...});

but it should be-但它应该是——

btnNext.addEventListener("click", () => {...});

Here is the working demo-这是工作演示-

 let btnNext = document.querySelector(".fa-circle-arrow-right"); let btnprevious = document.querySelector(".fa-circle-arrow-left"); btnNext.addEventListener("click", () => { document.getElementById("reviewtext").textContent = "Nu am reusit sa ajung la cursurile mele de algebra din cause serviciului.In orice caz,nu a fost mare pierdere deoarece Pitagora.ro ma ajutat sa recuperez materia fara probleme"; document.getElementById("reviewnume").textContent = "Bogdan,22"; document.getElementById("reviewnume").style.textAlign = "center"; document.getElementById("reviewtext").style.fontSize = "25px"; document.getElementById("reviewtext").style.marginLeft = "20px"; document.getElementById("student").src = "https://picsum.photos/id/237/200/300"; }); btnprevious.addEventListener("click", () => { document.getElementById("student").src = "https://picsum.photos/200/300"; });
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" integrity="sha512-MV7K8+y+gLIBoVD59lQIYicR65iaqukzvf/nwasF0nqhPay5w/9lJmVM2hMDcnK1OnMGCdVK+iQrJ7lzPJQd1w==" crossorigin="anonymous" referrerpolicy="no-referrer" /> <div class="review1"> <img id="student" src="https://picsum.photos/200/300" /> <div id="review1text"> <h1 id="reviewnume">Alina,21</h1> <p id="reviewtext"> Sunt studenta la Politehnica in Bucuresti si Pitagora.ro ma ajutat foarte mult sa aprofundez cunostiintele mele de algebra.Recomand cu cea mai mare incredere! </p> </div> <i class="fa-solid fa-circle-arrow-left"></i> <i class="fa-solid fa-circle-arrow-right"></i> </div>

Try this code试试这个代码

const image = document.getElementById("student");

image.setAttribute("src","images\review2.png");

If it doesn't work check the path or the type of image is png or not.如果它不起作用,请检查路径或图像类型是否为 png。

change document.getElementById("student").src = "images\review2.png";更改 document.getElementById("student").src = "images\review2.png"; to

document.getElementById("student").setAttribute('src', "images\review2.png"); document.getElementById("student").setAttribute('src', "images\review2.png");

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM