简体   繁体   English

jQuery切换img属性

[英]Jquery toggle img attribute

I am trying to toggle images from the thumbnail to the feature image when the thumb nail is clicked. 单击缩略图时,我试图将图像从缩略图切换为功能图像。 Currently when its clicked the images will swap but I cant get them to swap back with the thumb nail is clicked on again. 目前,当其单击时,图像将交换,但是我无法通过拇指指甲再次使它们交换回去。 I've tried using toggle but when the thumb nail is clicked on it would remove the image completely and I couldnt get any image to return. 我已经尝试过使用切换,但是当单击指甲时,它将完全删除图像,并且我无法返回任何图像。 Currently this will switch the images but not switch or toggle them back on click. 当前,这将切换图像,但不会在单击时切换或切换回它们。

$(".browseTable").on('click', 'td', function () {                

    var thumbNail = $(this).parent('tr').find('img').attr('src');
    var feature = $('#featureImg img').attr('src');

    $('#featureImg img').fadeOut(400, function () {
        $(this).fadeIn(400)[0].src = thumbNail;
    });
});

You can achieve your target via setting src of image in css. 您可以通过在CSS中设置图片的src来实现目标。

    .imageOne{
        content:url(YOUR SOURCE);
    }

    .imageTwo{
        content:url(YOUR SOURCE);
}

add class to your image element in start check if element exist by either class if exist toggle() to another, this will change the image back to another. 在开始检查时将类添加到图像元素中,如果存在,则该类是否存在于另一个类中(如果存在toggle()则切换到另一个),这会将图像更改回另一个。 And use toggle() like following example 然后像下面的例子一样使用toggle()

$(".browseTable").on('click', 'td', function () {                

            //var thumbNail = $(this).parent('tr').find('img').attr('src');
            //var feature = $('#featureImg img').attr('src');

            //$('#featureImg img').fadeOut(400, function () {
            //    $(this).fadeIn(400)[0].src = thumbNail;
            //});               

            $('#featureImg img').find('.imageOne, .imageTwo').toggle();
        });

I am able to use toggle simply to change one element to another. 我能够仅使用切换将一个元素更改为另一元素。 etc. hope it will help you. 等等,希望对您有所帮助。

Is it possible to set the equivalent of a src attribute of an img tag in CSS? 是否可以在CSS中设置img标签的src属性的等效项?

Try this. 尝试这个。 When the parent is clicked we toggle both children. 单击父级时,我们切换两个子级。 The large version starts off hidden. 大版本开始隐藏。

<div class="toggle js-toggle">
    <img src="http://www.placehold.it/100x100" alt="" class="toggle__thumb js-toggle-image"/>
    <img src="http://www.placehold.it/500x500" alt="" class="toggle__active js-toggle-image" style="display: none" />
</div>

The jQuery jQuery的

$('.js-toggle').on('click', function() {

    $(this).find('.js-toggle-image').toggle();

});

https://jsfiddle.net/uoLv2nkh/1/ https://jsfiddle.net/uoLv2nkh/1/

Or an only CSS way if you only care about when the user is clicking. 如果您只在乎用户单击的时间,则这是唯一的CSS方式。

https://jsfiddle.net/uoLv2nkh/ https://jsfiddle.net/uoLv2nkh/

You can use data for store source string and toggling images 您可以将数据用于存储源字符串和切换图像

preview on : https://jsfiddle.net/5kxf65Lx/ 预览于: https : //jsfiddle.net/5kxf65Lx/

<img src="source1.jpg" data-srcfirst="source1.jpg"     data-srcsecond="source2.jpg" />

<script type="text/javascript">

$('img').click(function(){
    var loaded = $(this).attr('src')

    var first = $(this).data('srcfirst')
    var second = $(this).data('srcsecond')

    if(loaded == first){
        $(this).attr('src' , second)
    } else {
        $(this).attr('src' , first)
    }

})

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

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