简体   繁体   English

我想一次更改 2 个图像

[英]I want to change 2 images at once

I have images in imageBox1 and imageBox2 and when I click button, is there any way that I can change images in both boxes at the same time?我在 imageBox1 和 imageBox2 中有图像,当我单击按钮时,有什么方法可以同时更改两个框中的图像? with JS?用JS? For instance, I when I click "hello", I want to make image1-1 and image2-1 display together, and when I click "world", they image1-2 and image2-2 should display together.比如我点击“hello”时,我想让image1-1和image2-1一起显示,而当我点击“world”时,image1-2和image2-2应该一起显示。

    <div class = "Button">
        <button type="button">hello</button>
        <button type="button">world</button>
    </div>

    <div class = "imageBox1">
        <img src="image1-1.jpg">
        <img src="image1-2.jpg">
    </div>

    <div class = "imageBox2>
        <img src="image2-1.jpg">
        <img src="image2-2.jpg">
    </div>

It sounds like what you're asking for is a way to show or hide specific images depending on which button the user clicks.听起来您要求的是一种根据用户单击的按钮来显示或隐藏特定图像的方法。 If so, you can definitely do this with JavaScript.如果是这样,你绝对可以用 JavaScript 做到这一点。

Here's a beginner-friendly example using your HTML.这是一个使用 HTML 的初学者友好示例。

 // set up some variables to make the code easier to read const helloBtn = document.getElementById("hello-button") const worldBtn = document.getElementById("world-button") const img1 = document.getElementById("image1") const img2 = document.getElementById("image2") const img3 = document.getElementById("image3") const img4 = document.getElementById("image4") // When the user clicks the "hello" button, run a function named handleHelloClick helloBtn.addEventListener("click", handleHelloClick); // When the user clicks the "world" button, run a function named handleWorldClick worldBtn.addEventListener("click", handleWorldClick); // this function handles the "hello" click function handleHelloClick() { img1.style.display = "none"; img2.style.display = "block"; img3.style.display = "block"; img4.style.display = "none"; } // this function handles the "world" click function handleWorldClick() { img1.style.display = "block"; img2.style.display = "none"; img3.style.display = "none"; img4.style.display = "block"; }
 <div class="buttons"> <button id="hello-button" type="button">hello</button> <button id="world-button" type="button">world</button> </div> <div class="imageBox1"> <img id="image1" src="https://randomuser.me/api/portraits/women/75.jpg"> <img id="image2" src="https://randomuser.me/api/portraits/men/76.jpg"> </div> <div class="imageBox2"> <img id="image3" src="https://randomuser.me/api/portraits/women/78.jpg"> <img id="image4" src="https://randomuser.me/api/portraits/men/80.jpg"> </div>

first hide them with css then show them when clicking by js首先用 css 隐藏它们,然后在通过 js 单击时显示它们

 const hello = document.getElementById("hello") const world = document.getElementById("world") hello.onclick= () => { document.querySelectorAll(".first").forEach(el=> el.style.display = "block") } world.onclick= () => { document.querySelectorAll(".second").forEach(el=> el.style.display = "block") }
 .first, .second { display : none }
 <div class = "Button"> <button id="hello" type="button">hello</button> <button id="world" type="button">world</button> </div> <div class = "imageBox1"> <img class="first" src="https://dummyimage.com/200x100/000/fff&text=first"> <img class="second" src="https://dummyimage.com/200x100/000/fff&text=second"> </div> <div class = "imageBox2"> <img class="first" src="https://dummyimage.com/200x100/000/fff&text=first"> <img class="second" src="https://dummyimage.com/200x100/000/fff&text=second"> </div>

You can add an event listener to each button that gets the index of the button relative to the other buttons, loops through each child in each div and shows the img whose index is the same as the button's.您可以向每个按钮添加一个事件侦听器,以获取按钮相对于其他按钮的索引,循环遍历每个div每个子项并显示其索引与按钮相同的img

 const divs = document.querySelectorAll('.image'); const buttons = document.querySelectorAll('button') buttons.forEach((e, i) => e.addEventListener('click', function() { divs.forEach((f) => [...f.children].forEach((g, i1) => g.style.display = i == i1 ? "block" : "none")) }))
 img { display: none; }
 <div class="Button"> <button type="button">hello</button> <button type="button">world</button> </div> <div class="imageBox1 image"> <img src="https://www.gravatar.com/avatar/0fdacb141bca7fa57c392b5f03872176?s=48&d=identicon&r=PG&f=1"> <!-- image1-1.jpg --> <img src="https://www.gravatar.com/avatar/0fdacb141bca7fa57c392b5f03872176?s=96"> <!-- image1-2.jpg --> </div> <div class="imageBox2 image"> <img src="https://www.gravatar.com/avatar/0fdacb141bca7fa57c392b5f03872176?s=96"> <!-- image2-1.jpg --> <img src="https://www.gravatar.com/avatar/0fdacb141bca7fa57c392b5f03872176?s=48&d=identicon&r=PG&f=1"> <!-- image2-2.jpg --> </div>

暂无
暂无

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

相关问题 我有7张图片。 我想使用javascript向不同的用户随机显示不同的图像。 但是一旦显示,它就不会改变 - I have 7 images. I want to display different images to different users randomly using javascript. But once it is displayed, it should not change 我想再次更改2张图片onmouseover和agian - i want to change 2 images onmouseover again and agian 我想在 ionic 3 中打开相机后拍摄多张图像 - I want to capture multiple images once open camera in ionic 3 我想更改一次按钮的背景色,一旦我单击另一个按钮就再次更改为原始颜色 - i want to change the background color of a button once clicked and change again to original color once i click another button 我有四个div,每个div包含2张图片。 我想随着每个div更改这些图片,但不要一次 - I had Four divs contains 2 images in each div. I want to alter those Images withing each div, BUT NOT AT ONCE 我正在制作纸牌游戏,我想在点击后更改图像 - I'm making a card game and I want to change the image once it has been clicked jQuery .on(“ change”)运行越来越多,但我只希望它运行一次 - jQuery .on(“change”) running more and more times but I only want it to run once 有没有办法在JS中一次更改少量图像的src? - Is there a way to change src of few images at once in JS? 我想要上传更多图片 - i want more images upload 要在单击按钮上以HTML随机更改图像 - want to change images randomly in HTML on Click on button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM