简体   繁体   English

用onClick更改onClick

[英]Changing onClick with onClick

For a website I am coding, I have an interface where one spot holds 4 images: One for Standard, Hover, Clicked, and Bought. 对于我正在编码的网站,我有一个界面,其中一个点可以容纳4张图像:一个用于标准,悬停,点击和购买。 The way it currently works is that when the Standard image is hovered over, it switches to the Hover image. 当前的工作方式是,当将标准图像悬停时,它将切换到悬停图像。 When the Hover image is clicked, it switches to the Clicked image. 单击悬停图像后,它将切换到“单击的图像”。 But I have been struggling to get the Bought image to show up when clicked. 但是我一直在努力使“购买的”图像在单击时显示出来。 Code: 码:

     <script>
        function on1() {
            var x = document.getElementById("1");
            x.src = "Hover1.png"; 
            x.style.width = "31%";
        }   
        function out1() {
            var x = document.getElementById("1");
            x.src = "Item1.png"; 
            x.style.width = "30%";
        }
    </script>
    <img src="Item1.png" width="30%" id="1" 
        onmouseover="on1();" 
        onmouseout="out1();" 
        onclick="this.src = 'Desc1.png';">`

Help would be appreciated 帮助将不胜感激

You need to add a function handler for the event as follows: 您需要为事件添加一个函数处理程序,如下所示:

HTML CODE: HTML代码:

<img src="Item1.png" width="30%" id="1" onclick="updateImage(this)">

JS CODE: JS代码:

updateImage = function(el){
    el.src = 'Desc1.png';
}

I don't know what "Bought" means actually , but I saw that you're trying to make the initial image from the begining be the one in this "Bought" phase. 我不知道“购买”实际上是什么意思,但是我看到您正在尝试从一开始就使初始图像成为“购买”阶段中的图像。

So a jquery code should do it : 所以一个jQuery代码应该做到这一点:

$(document).ready(function(){
    var initial ='https://pbs.twimg.com/profile_images/587778453937618944/-z_faB4f.jpg';
    var hovering = 'http://www.gettyimages.fr/CMS/Pages/Embed_Frontdoor/StaticContent/hero_travel_482206371.jpg';
    var clicking = 'http://www.menucool.com/slider/prod/image-slider-2.jpg';
    $('#test')
    .attr('src',initial)
    .hover(function(ev){
        $(this).attr('src',hovering);
    })
    .mousedown(function(ev){
        $(this).attr('src',clicking);
    })
    .mouseup(function(ev){
        $(this).attr('src',initial);
    })
    .mouseout(function(ev){
        $(this).attr('src',initial);
    });
});

Here's a fiddle 这是一个小提琴

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

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