改变儿童形象风格

时间:2015-07-19 16:21:32

标签: javascript image-gallery

我将此脚本用于简单的图库。在最后一行,我试图在'this'中更改图像的边框,但样式由于某种原因没有改变。我做错了什么?

$('.gallery-image').on('click',function(){
    var bigCurrent = $('.hero-image').attr('src'); //the current large image src
    var bigThumb = $('.hero-image').attr('data-smSrc'); //the thumb version src of the current large image
    var thumbSm = $(this).attr('src'); //the currently being clicked on thumb's src
    var thumbLgSrc = $(this).attr('data-lgSrc'); //the large version src of the currently being clicked on thumb

    $('.hero-image').attr('src', thumbLgSrc);
    $('img', this).css('border','2px solid #fff');

});

更新: 这是有效的,并遵循以下一些评论中的更正

$('.gallery-image').on('click',function(){
    var bigCurrent = $('.hero-image').attr('src'); //the current large image src
    var bigThumb = $('.hero-image').attr('data-smSrc'); //the thumb version src of the current large image
    var thumbSm = $('img', this).attr('src'); //the currently being clicked on thumb's src
    var thumbLgSrc = $(this).attr('data-lgSrc'); //the large version src of the currently being clicked on thumb

    $('.hero-image').attr('src', thumbLgSrc);

    $('.selected').removeClass('selected');

     $('img', this).addClass('selected'); 
});

1 个答案:

答案 0 :(得分:0)

如果没有看到这种失败,你可能会选择错误的东西。

您有$(this).attr('src'),这意味着this是图像节点本身,因为该属性特定于img。然而,您尝试在img上下文中选择this,并且由于img无法生育孩子,因此不太可能有img个节点找到。

请尝试将其更改为$(this).css(...)