簡體   English   中英

如何通過使用onclick單擊將圖像更改為其他圖像?

[英]How do i change an image into a different image by clicking on it with onclick?

如何通過使用onclick單擊將圖像更改為其他圖像?

#box{ height:200px; width:200px; background-color:gold; }
<body>
    <div id="box"></div>
    <script type="text/javascript">
        document.getElementById("box").onclick = function() {
            document.getElementById("box").style.display = ???? 
        }
    </script>
</body>

更新html元素的style屬性中backgroundImage屬性

document.getElementById("image1").onclick = function() {
   this.style.backgroundImage = 'url(img.jpg)'
}

 #box { height: 200px; width: 200px; background-color: gold; } 
 <div id="box"></div> <script type="text/javascript"> document.getElementById("image1").onclick = function() { this.style.backgroundImage = 'url(img.jpg)'; } </script> 

使用jQuery:

$('your_image_id_or_selector').click(function(){
   $(this).attr('src','new_image_url');
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM