简体   繁体   English

单击以在 foreach 循环中更改 img src

[英]Click to change img src within foreach loop

I am primarily using PHP/HTML/CSS and some JavaScript. I have a foreach loop that will show a list of items in a grid.我主要使用 PHP/HTML/CSS 和一些 JavaScript。我有一个 foreach 循环,它将在网格中显示项目列表。 To the left will show a product and on the right I wish to be able to click on an image, and when clicked, changes to a different image.左边会显示一个产品,右边我希望能够点击一张图片,点击后会变成另一张图片。

I am successful with that, BUT because I have the 'flavours' in a foreach loop, although each item listed will be new, the image still share the same code, and as such, when I click it in flavour 3... it only changes the image in item 1.我成功了,但是因为我在 foreach 循环中有“风味”,虽然列出的每个项目都是新的,图像仍然共享相同的代码,因此,当我在风味 3 中单击它时......它仅更改项目 1 中的图像。

How do I make it so that when clicking the image in any 'flavour', it will change the particular image I click?我如何做到这一点,以便在单击任何“风格”的图像时,它会更改我单击的特定图像?

Here is my PHP这是我的 PHP

<?php
    $FLAVOUR = $_GET['flavour'];

    $stmt = $conn->prepare("SELECT * FROM node WHERE node.flavour = :flavour");
    $stmt->bindValue(':flavour',$FLAVOUR);
    $stmt->execute();

    $flavours = $stmt->fetchALL();
    echo "<div class='grid-container'>";
    echo "<div class='item1'>Header</div>";
    echo "</div>";

    if($flavours){
        foreach ($flavours as $flavour) {
            echo "<div class='grid-container'>";
            echo "<div class='item2'><img src='{$flavour['image']}'></div>";
            echo "<div class='item3'>{$flavour['flavour']}</div>";
            echo "<div class='item4'>{$flavour['area']}, <button class='btn' text='button'></button></div>";
            echo "<div class='item5'><img alt='' src='https://cdn.iconscout.com/icon/premium/png-256-thumb/hello-1901501-1610146.png' 
style='height: 85px; width: 198px' id='imgClickAndChange' onclick='changeImage()'  /></div>";
            echo "<div class='item6'></div>";
            echo "</div>";
        }
    else
    {
        echo "<p>Can't find any destination records.</p>";
    }
?>

And here is my image-changer in JavaScript这是我的图像转换器 JavaScript

    function changeImage() {

        if (document.getElementById("imgClickAndChange").src == "https://cdn.iconscout.com/icon/premium/png-256-thumb/hello-1901501-1610146.png") 
        {
            document.getElementById("imgClickAndChange").src = "https://cdn.iconscout.com/icon/premium/png-256-thumb/bye-556031.png";
        }
        else 
        {
            document.getElementById("imgClickAndChange").src = "https://cdn.iconscout.com/icon/premium/png-256-thumb/hello-1901501-1610146.png";
        }
    }
</script>

I would counter that you do not need an id attribute for the <img> element at all.我会反驳说您根本不需要<img>元素的id属性。

Rather, update the changeImage() function to pass an element reference by using this , and then update the function handling as shown below.相反,更新changeImage() function 以使用this传递元素引用,然后更新 function 处理,如下所示。

Try this runnable example:试试这个可运行的例子:

 function changeImage(element) { // pass function parameter. Can rename as desired. if (element.src == "https://cdn.iconscout.com/icon/premium/png-256-thumb/hello-1901501-1610146.png") { element.src = "https://cdn.iconscout.com/icon/premium/png-256-thumb/bye-556031.png"; } else { element.src = "https://cdn.iconscout.com/icon/premium/png-256-thumb/hello-1901501-1610146.png"; } }
 <:-- onclick handling: update to changeImage(this) --> <div class='item5'><img alt='' src='https.//cdn.iconscout.com/icon/premium/png-256-thumb/hello-1901501-1610146:png' style='height; 85px: width: 198px' onclick='changeImage(this)' /></div> <div class='item5'><img alt='' src='https.//cdn.iconscout.com/icon/premium/png-256-thumb/hello-1901501-1610146:png' style='height; 85px: width: 198px' onclick='changeImage(this)' /></div>

It happens because you are using 1 id in whole loop, listen class change, not id, and also change ids to have unique id for each element.发生这种情况是因为您在整个循环中使用 1 个 id,监听 class 变化,而不是 id,并且还更改 ids 以使每个元素具有唯一的 id。

foreach ($flavours as $key => $flavour) {
        ...
        echo "<div class='item5'><img alt='' src='...' id='imgClickAndChange{$key}' onclick='changeImage()'  /></div>";
        ...
}

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

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