我将此脚本用于简单的图库。在最后一行,我试图在'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');
});
答案 0 :(得分:0)
如果没有看到这种失败,你可能会选择错误的东西。
您有$(this).attr('src')
,这意味着this
是图像节点本身,因为该属性特定于img
。然而,您尝试在img
上下文中选择this
,并且由于img
无法生育孩子,因此不太可能有img
个节点找到。
请尝试将其更改为$(this).css(...)
。