2012-05-11 41 views
0

我在这里有一个类似的问题: jQuery function for specific class 我解决了。但它让我感到困扰,因为它有太多的div,它看起来不太好,所以我重写了我的HTML代码并重写了选择器脚本。 现在该脚本加载图像很好(它会将它们全部淡入),但选择完全不起作用。 我尝试使用最接近和兄弟功能,但无济于事。问题与jQ图像选择器

我该如何解决这个问题? 您可以找到页面中的问题: http://baldino.rs/baby-program/

Thanx提前

$(document).ready(function(){ 

var picture = $('.post-cipela').each(function(index, element) { 
$(this).find('.cipela-bg img:eq(0)').fadeIn(500); 

$('.colorwrap a').click(function(){ 
    var index = $(this).find(".colorwrap a").index(this); 
    $('.cipela-bg img').fadeOut(200); 
    $('.cipela-bg img:eq('+index+')').fadeIn(500); 
    }); 
}); 

编辑-1: 我修改我的脚本。现在我有一个问题,因为我的图像多次褪色。我该如何解决它? - 下面是修改后的脚本,并在那里你可以看到问题的网页是在这里: http://baldino.rs/baby-program

$(document).ready 
(
function() 
{ 
$(".cipela-1").fadeIn(200); 
$(".colorwrap a").click 
(
    function() 
    { 
     var item = $(this); 
     var a = item.attr("rel"); 
     item.closest(".post-cipela").find(".cipela-1, .cipela-2, .cipela-3, .cipela- 
     4").fadeOut(200); 
     item.closest(".post-cipela").find("."+a).first().fadeIn(200); 

    } 
); 
} 
); 

回答

1

你,你粘贴代码中伤,你到底有没有额外的});。另外,你在你的.each函数循环中打包$('。colorwrap a')选择器,我不确定你的意思。

此外,您已经错过了一些关于确定这个变量的范围。

这条线在你的each很好。

$(this).find('.cipela-bg img:eq(0)').fadeIn(500); 

但你实例化一个单击处理

$('.colorwrap a').click(function(){ 
    var index = $(this).find(".colorwrap a").index(this); 

的$(本),其处理程序中是指匹配a.colorwrap。然后你在下面找到另一个.colorwrap a实例,这可能不存在,因此你的选择器没有找到任何东西。

如果你确实打算在这个包裹单击处理程序上的每个迭代。每次,你应该将循环内分配$(this)给一个变量,然后单击处理程序中使用这样的

var picture = $('.post-cipela').each(function(index, element) { 
    var that =$(this); 
    that.find('.cipela-bg img:eq(0)').fadeIn(500); 

    $('.colorwrap a').click(function(){ 
     var index = that.find(".colorwrap a").index(this); 
     $('.cipela-bg img').fadeOut(200); 
     $('.cipela-bg img:eq('+index+')').fadeIn(500); 
    }); 
}); 
+0

其实,我完全开始在错误的脚上写脚本,阅读我的帖子编辑。 –

+0

我修好了。 Thanx在正确的位置指引我。远离每个()即是。 –