2013-04-11 86 views
0

我確定這對大多數人來說很簡單,但我很努力學習這個概念。我只需要一點幫助。我已經評論足以解釋發生了什麼。謝謝!我需要將一個變量傳遞給我的函數

//tabNavItem is an anchor link 
$(tabNavItem).on("click", function(e){ 
    var currSlide = $(this).attr("rel"); 
    console.log(currSlide); // right here the value is equal to the rel of the link i click. this is correct!     
    slideAnimation(); // i tried slideAnimation.call(currSlide) but got nothing and i tried slideAnimation(currSlide) and got 1 every time 
    e.preventDefault(); 

}); 

function slideAnimation(){ 

    allTabs.hide(); 
    $(".active").removeClass("active"); 
    $("#" + currSlide).show();       
    $("." + tabNavClass + " a[rel=" + currSlide + "]").addClass('active');   

    console.log(currSlide); //right now this equals 1, the rel of the first item. 

}; 

回答

3

您必須聲明功能實際上接受一個參數,像這樣:

function slideAnimation(currSlide){ 

然後,您可以傳遞參數時稱之爲

slideAnimation(currSlide); 

如果您不知道,請注意,JavaScript不會嘗試確保參數的類型,並且參數不必與要傳遞的值具有相同的名稱。

+0

謝謝!我在一個點上嘗試過每一個,猜想我只是沒有在同一時間做。感謝幫助我!我會盡快接受! – heyjohnmurray 2013-04-11 03:31:32

0

提示:一個參數添加到slideAnimation

function slideAnimation (currSlide){ 
    // snip 
} 
1
//Add an argument here 
function slideAnimation(item){ 
    allTabs.hide(); 
    $(".active").removeClass("active"); 
    $("#" + item).show();       
    $("." + tabNavClass + " a[rel=" + currSlide + "]").addClass('active');   
}; 

//then call it like this 
$(tabNavItem).on("click", function(e){ 
    var currSlide = $(this).attr("rel"); 
    slideAnimation(currSlide); 
    e.preventDefault(); 
});