简体   繁体   English

jQuery的切换图像无法正常工作

[英]JQuery Toggle Image not working

I am simply trying to toggle the image source with a click using JQuery. 我只是试图使用JQuery单击来切换图像源。 My script is below, I am not sure why it's not toggling. 我的脚本在下面,我不确定为什么不切换。 When the page first loads, if you click the gray image, it toggles to the color image. 首次加载页面时,如果单击灰色图像,它将切换到彩色图像。 But if you click the gray image, nothing happens. 但是,如果单击灰色图像,则什么也不会发生。 So it changes on the first click but nothing happens if you click after the first click. 因此,它在第一次单击时就发生了变化,但是如果您在第一次单击后单击就没有任何反应。

<script type="text/javascript">
    $(document).ready(function () {
        $('#imageid').click(function () {
            if ($(this).attr('src', 'imagegray.png')) {
                $(this).attr('src', 'imagecolor.png');
            }
            else if ($(this).attr('src', 'imagecolor.png')) {
                $(this).attr('src', 'imagegray.png');
            }
        })
    });
</script>

You need to compare src attribute. 您需要比较src属性。 Currently you are setting them in if's condition block 目前,您正在将它们设置在if条件块中

 $('#imageid').click(function () {
     if ($(this).attr('src') === 'imagegray.png') {
         $(this).attr('src', 'imagecolor.png');
     }
     else if ($(this).attr('src') === 'imagecolor.png') {
         $(this).attr('src', 'imagegray.png');
     }
 })

You need to get the source value of your image then compare it with the expected name that you want, currently you're doing it wrongly, change your code to: 您需要获取图像的源值,然后将其与所需的期望名称进行比较,当前操作有误,请将代码更改为:

$('#imageid').click(function () {
    if ($(this).attr('src') == 'imagegray.png') {
        $(this).attr('src', 'imagecolor.png');
    }
    else if ($(this).attr('src') == 'imagecolor.png') {
        $(this).attr('src', 'imagegray.png');
    }
})

Fiddle Demo 小提琴演示

Other suggestions will achieve what you want, but I believe that it'll get better if you work with classes 其他建议可以实现您想要的目标,但是我相信,如果您上课的话,它将变得更好

For example, your image will always have the active class and when you click will insert / delete the inactive class: 例如,您的图像将始终具有active类,并且当您单击时将插入/删除inactive类:

$(this).toogleClass('inactive');

Here's the same thing done in few different ways. 这是通过几种不同方式完成的同一件事。 They all do the same thing, with varying degrees of compactness. 它们都以不同的紧凑度执行相同的操作。

The last one is probably most convenient and the most flexible one. 最后一个可能是最方便,最灵活的一种。 You never have to touch your javascript to change the image src values. 您无需触摸JavaScript即可更改图片的src值。


http://jsfiddle.net/yPAqY/ http://jsfiddle.net/yPAqY/

Html: HTML:

<img id="imageid" src="imagegray.png" alt="">

jQuery: jQuery的:

$(document).ready(function () {

    $('#imageid').on("click", function () {

        var obj = $(this),
            objAttr = obj.attr('src'),
            first_img = 'imagegray.png',
            second_img = 'imagecolor.png';

        if (objAttr === first_img) {
            obj.attr('src', second_img);
        } else if (objAttr === second_img) {
            obj.attr('src', first_img);
        }

    });

});

http://jsfiddle.net/yPAqY/1 http://jsfiddle.net/yPAqY/1

Html: HTML:

<img id="imageid" src="imagegray.png" alt="">

jQuery: jQuery的:

$(document).ready(function() {

    $('#imageid').on("click", function() {

        var obj = $(this),

            first_img = 'imagegray.png',
            second_img = 'imagecolor.png',

            imgs = obj.attr('src') == first_img ? second_img : first_img;

        obj.attr('src', imgs);

    });

});

http://jsfiddle.net/yPAqY/2/ http://jsfiddle.net/yPAqY/2/

Html: HTML:

<img id="imageid" src="imagegray.png" data-img2="imagecolor.png" alt="">

jQuery: jQuery的:

$(document).ready(function() {

    var image = $('#imageid'),
        imageSrc = image.attr('src'),
        imageData = image.data('img2');

    image
        .removeAttr('data-img2')
        .on("click", function() {

            var obj = $(this),
                imgs = obj.attr('src') == imageSrc ? imageData : imageSrc;

            obj.attr('src', imgs);

        });

});

place two images in seperate class 将两个图像放在单独的类中

and toggle the class you can see the effect 并切换班级,您可以看到效果

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

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