2010-09-23 103 views
0
循環

嗨,我想打在有階級的「.block儲液標籤」的所有元素的功能循環,並做到以下幾點:通過元素

$(function(){ 

function cssTabs(){ 

var firstTab = $(".block-header-tabs").find("a:first"); 
var firstBlock = $(".block-header-tabs").find("a:first").attr('href'); 
$(firstBlock).parent().css({position: "relative"}); 
$(firstBlock).css({position: "absolute", zIndex: "2"}) 
$(firstBlock).siblings().css({opacity: "0", position: "absolute", top: "0", zIndex: "1"}); 
$(firstTab).addClass("tab-current"); 
$(firstTab).siblings().addClass("tab-noncurrent"); 

} 

cssTabs(); 

$(".block-header-tabs a").click(function(){ 
    $(this).siblings().removeClass("tab-current").addClass("tab-noncurrent"); 
    $(this).removeClass("tab-noncurrent").addClass("tab-current") 
    var clickedTab = $(this).attr('href'); 
    $(clickedTab).siblings().css({zIndex: "1"}).stop(0,0).animate({opacity:"0"}, function(){ 
     $(clickedTab).siblings().css({display:"none"}); 
    }); 
    $(clickedTab).css({display:"block", zIndex:"2"}).stop(0,0).animate({opacity:"1"}); 

    return false; 

}); 

}); 

這裏是生活的鏈接例如,所以你可以檢查出來自己:) http://dl.dropbox.com/u/2878602/moviezet/index.html

感謝

+0

你的問題是什麼? – Reigel 2010-09-23 06:55:42

+0

通過解釋鏈接的方式,您可以看到該函數僅適用於.block-header-tabs的第一個實例,而不是第二個實例的右下方。它不是樣式,因爲它應該是:( – Fez 2010-09-23 06:56:11

+0

我的問題是,我希望該功能重複,以便它爲所有元素的樣式,目前它只在第一個實例.block-header-tabs – Fez 2010-09-23 07:06:08

回答

0

嘿,我自己解決了這個問題,事實證明,我不能使用a:首先定位主塊,如果我想在它上面使用.each()。 :)這裏是固定的代碼,如果有人有興趣:

$(".block-header-tabs").each(function(){ 
    $($(this).find("a:first").attr('href')).css({position: "absolute", zIndex: "2"}) 
    $($(this).find("a:first").attr('href')).siblings().css({opacity: "0", position: "absolute", top: "0", zIndex: "1"}); 
    $($(this).find("a:first").attr('href')).parent().css({position: "relative"}); 
}); 

感謝:d

0

嘗試這個

$(function(){ 
    function cssTabs($this){ 
    var firstTab = $this(".block-header-tabs").find("a:first"); 
    var firstBlock = $this(".block-header-tabs").find("a:first").attr('href'); 
    $(firstBlock).parent().css({position: "relative"}); 
    $(firstBlock).css({position: "absolute", zIndex: "2"}) 
    $(firstBlock).siblings().css({opacity: "0", position: "absolute", top: "0", zIndex: "1"}); 
    $(firstTab).addClass("tab-current"); 
    $(firstTab).siblings().addClass("tab-noncurrent"); 
    } 

    $('.block-header-tabs').each(function(index) { 
    cssTabs($(this)); 
    $(this).find("a").click(function(){ 
     $(this).siblings().removeClass("tab-current").addClass("tab-noncurrent"); 
     $(this).removeClass("tab-noncurrent").addClass("tab-current") 
     var clickedTab = $(this).attr('href'); 
     $(clickedTab).siblings().css({zIndex: "1"}).stop(0,0).animate({opacity:"0"}, function(){ 
      $(clickedTab).siblings().css({display:"none"}); 
     }); 
     $(clickedTab).css({display:"block", zIndex:"2"}).stop(0,0).animate({opacity:"1"}); 
     return false; 
    }); 
    }); 
}); 
+0

嗯它似乎打破了一切?:S – Fez 2010-09-23 07:12:38

+0

26未知類型錯誤:對象不是函數 cssTabs // - private-/scripts/site-functions.js:26 (匿名函數)/ - private-/scripts/site-functions.js :36 eachajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js:30 c.fn.c.eachajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery .min.js:24 (匿名函數)// - private-/scripts/site-functions.js:35 readyajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js:26 L – Fez 2010-09-23 07:17:03

+0

調試你的腳本,它應該工作,主要想法是$('。block-header-tabs')。each(function(index){});哪個循環 – 2010-09-23 07:31:49

0
function cssTabs(element){ 
    var firstTab = element.find("a:first"); 
    var firstBlock = element.find("a:first").attr('href'); 
    $(firstBlock).parent().css({position: "relative"}); 
    $(firstBlock).css({position: "absolute", zIndex: "2"}) 
    $(firstBlock).siblings().css({opacity: "0", position: "absolute", top: "0", zIndex: "1"}); 
    $(firstTab).addClass("tab-current"); 
    $(firstTab).siblings().addClass("tab-noncurrent"); 
} 



$(function(){ 
    $.each($('.block-header-tabs'), function(i, el){ 
     cssTabs($(this)); 
     $(this).find('a').click(function(){ 
      $(this).siblings().removeClass("tab-current").addClass("tab-noncurrent"); 
      $(this).removeClass("tab-noncurrent").addClass("tab-current"); 
      var clickedTab = $(this).attr('href'); 
      $(clickedTab).siblings().css({ 
       zIndex: "1" 
      }).stop(0,0).animate({ 
       opacity:"0" 
      }, function(){ 
       $(clickedTab).siblings().css({display:"none"}); 
      }); 
      $(clickedTab).css({display:"block", zIndex:"2"}).stop(0,0).animate({opacity:"1"}); 
      return false; 
     })  
    }); 
}); 
+0

未捕獲TypeError:對象不是函數 – Fez 2010-09-23 07:15:06