简体   繁体   English

单击按钮时如何更新 html(单击按钮时更改图像)

[英]how do i update the html when a button is clicked (change image when button is clicked)

I'm trying to update the html when a button is clicked.单击按钮时,我正在尝试更新 html。

I have tried to solve this issue for a few hours now and I don't know if I'm stupid but the images are not changing我已经尝试解决这个问题几个小时了,我不知道我是否愚蠢,但图像没有改变

 const slider = document.querySelector(".slider") const btn = document.querySelector(".next") const btn2 = document.querySelector(".previous") const images = ['car.jpg', `left.jpg`] window.addEventListener("load", iniliatizeSlider()) function iniliatizeSlider(){ var x = 0 cars = '' cars += `<div class="slide"> <img src="${images[x]}" alt"client"> <br><br> </div>` slider.innerHTML = cars; } btn.addEventListener("click", consoleMsg) btn2.addEventListener("click", consoleMsg2) function consoleMsg(){ x=1 } function consoleMsg2(){ x=0 }
 <section id="slider-section"> <div class="container"> <div class="subcontainer"> <div class="slider-wrapper"> <h2>client showcase</h2> <br /> <div class="slider"></div> <div id="controls"> <button class="previous"> <img src="left.jpg" alt="previous client" /> </button> <button class="next"> <img src="right.jpg" alt="next client" /> </button> </div> </div> </div> </div> </section>

I was expecting the image to change when the button was clicked but the image stayed the same but the value of x changed我原以为单击按钮时图像会发生变化,但图像保持不变但 x 的值发生了变化

Your initialize function is running once, when the page loads and at that point you are setting the image source to 0 and you never change it after that.当页面加载时,您的初始化 function 运行一次,此时您将图像源设置为 0,此后您永远不会更改它。 You need to adjust the image source within the functions that react to button clicks.您需要在对按钮点击做出反应的函数中调整图像源。 Now you do update x in those functions but nothing is ever done with x after that point.现在你在这些函数中更新x但在那之后没有对x做任何事情。

A couple of other things... With .addEventListener() , you pass a reference to the callback function, not invoke the function, so the line should be: window.addEventListener("load", iniliatizeSlider) <-- no () after the function name.其他一些事情......使用.addEventListener() ,您传递对回调 function 的引用,而不是调用 function,因此该行应该是: window.addEventListener("load", iniliatizeSlider) <-- no ()在 function 名称之后。

Also, you don't need to replace the HTML on the page to update the image, you only need to update the image's src property.另外,更新图片不需要替换页面上的HTML,只需要更新图片的src属性即可。

See comments below:请参阅以下评论:

 // Get a reference to an existing image element. // No need to recreate the <img> element. const img = document.querySelector(".slider img"); const next = document.querySelector(".next"); const prev = document.querySelector(".previous"); const images = ["https://cache.mrporter.com/content/images/cms/ycm/resource/blob/1252204/68e7f03297f41cb3ce41f15ec478f70f/image-data.jpg/w1500_q80.jpg", "https://play-lh.googleusercontent.com/VC7rta8PIK3MqmQG5c-F5CNJQ6cCv6Eb-kyBoUcQ2xj83dZVhn7YCj_GIWW8y7TnAMjU=w240-h480-rw"]; let currentIndex = 0; // Keeps track of which image is shown next.addEventListener("click", function(){ // Check to see if we're at the end of the array if(currentIndex === images.length-1){ currentIndex = 0; // Reset index } else { currentIndex++; // increase the index } img.src = images[currentIndex]; // Just update the existing image's source }); prev.addEventListener("click", function(){ // Check to see if we're at the beginning of the array if(currentIndex === 0){ currentIndex = images.length-1; // Reset index } else { currentIndex--; // decrease the index } img.src = images[currentIndex]; // Just update the existing image's source });
 img { width:50px; }
 <section id="slider-section"> <div class="container"> <div class="subcontainer"> <div class="slider-wrapper"> <h2>client showcase</h2> <br > <div class="slider"> <,-- Here. we just put a static image element with the first image we want to see: --> <img src="https.//cache.mrporter.com/content/images/cms/ycm/resource/blob/1252204/68e7f03297f41cb3ce41f15ec478f70f/image-data.jpg/w1500_q80.jpg"> </div> <div id="controls"> <button class="previous"> <img src="left.jpg" alt="previous client"> </button> <button class="next"> <img src="right.jpg" alt="next client"> </button> </div> </div> </div> </div> </section>

Call the function initializeSlider() on click of btn indtead of consoleMsg().单击 consoleMsg() 的 btn indtead 调用 function initializeSlider()。

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

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