“Ubah gambar pada klik JavaScript” Kode Jawaban

Contoh JavaScript Ubah OnClick

//in html
<img id="img" data-image="user1" src="/assets/user1.jpg" onclick="change_image()" />

//in javascript
const img = document.getElementById('img');
const change_image = function() {
  if (img.dataset.image == "user1") {
  	img.src = "/assets/user2.jpg";
    img.dataset.image = "user2";
    return
  };
  
  if (img.dataset.image == "user2") {
  	img.src = "/assets/user1.jpg";
    img.dataset.image = "user1";
    return
  };
};
Julio Polycarpo

Ubah Image Onclick JavaScript

<p>
    <img alt="" src="http://www.userinterfaceicons.com/80x80/minimize.png" 
        style="height: 85px; width: 198px" id="imgClickAndChange" onclick="changeImage()"/>
</p>
<script>
function changeImage() {
    if (document.getElementById("imgClickAndChange").src == "http://www.userinterfaceicons.com/80x80/minimize.png"){
        document.getElementById("imgClickAndChange").src = "http://www.userinterfaceicons.com/80x80/maximize.png";
    } else {
        document.getElementById("imgClickAndChange").src = "http://www.userinterfaceicons.com/80x80/minimize.png";
    }
}
</script>
ayaan

JS klik ubah img

function imagefun() {
            var Image_Id = document.getElementById('getImage');
            if (Image_Id.src.match("imageName1.jpg")) {
                Image_Id.src = "imageName2.jpg";
                Image_Id.src = "imageName3.jpg";
            }
            else {
                Image_Id.src = "imageName1.jpg";
            }
        }
alireza farezinmnsh

gambar perubahan js sur klik

let myImage = document.querySelector('img');

myImage.onclick = function() {
    let mySrc = myImage.getAttribute('src');
    if(mySrc === 'images/firefox-icon.png') {
      myImage.setAttribute('src','images/firefox2.png');
    } else {
      myImage.setAttribute('src','images/firefox-icon.png');
    }
}
Aziz YAGO

cara mengubah gambar pada mouse klik di javascript

<img src="imageName1.jpg" onmouseover="imagefun()" id="getImage">
Black Bat

Ubah gambar pada klik JavaScript

function changeImage() {
    if (document.getElementById("imgClickAndChange").src == "http://www.userinterfaceicons.com/80x80/minimize.png"){
        document.getElementById("imgClickAndChange").src = "http://www.userinterfaceicons.com/80x80/maximize.png";
    } else {
        document.getElementById("imgClickAndChange").src = "http://www.userinterfaceicons.com/80x80/minimize.png";
    }
}
krkmJR

Jawaban yang mirip dengan “Ubah gambar pada klik JavaScript”

Pertanyaan yang mirip dengan “Ubah gambar pada klik JavaScript”

Lebih banyak jawaban terkait untuk “Ubah gambar pada klik JavaScript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya