简体   繁体   English

更改子图像样式

[英]Change child image style

I am using this script for a simple image gallery. 我将此脚本用于简单的图片库。 On the last line, I am trying to change the border of the image within 'this' but the style is not changing for some reason. 在最后一行,我试图在“ this”中更改图像的边框,但是由于某种原因样式没有更改。 What have I done wrong? 我做错了什么?

$('.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');

});

UPDATE: This is working and follows the corrections made in some comments below 更新:这是可行的,并且遵循以下一些评论中所做的更正

$('.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'); 
});

Without seeing this failing, chances are, you are trying to select the wrong thing. 您没有选择失败的机会,而是尝试选择错误的事物。

You have $(this).attr('src') , which implies that this is the image node itself, because that attribute is specific to img . 您有$(this).attr('src') ,这意味着this是图像节点本身,因为该属性特定于img Yet you try to select an img within this context, and since img can't have children, it's unlikely there are any img nodes to find. 但是,您尝试在this上下文中选择一个img ,并且由于img不能有子级,因此不太可能找到任何img节点。

Try changing it to just $(this).css(...) instead. 尝试将其更改为仅$(this).css(...)

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

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