2011-07-09 16 views
0

我可以逆向工程jQuery,但是我現在必須學習如何轉發工程師。我知道如何編寫JavaScript函數,並且我知道在JavaScript函數中,您可以使用jQuery。不過,我真的堅持與努力使功能了以下代碼:幫我理解如何在jQuery中編寫函數

$(document).ready(function(){ 
    function doStuff(bar) 
    { 
     // Match all <A/> links with a title tag and use it as the content (default). 
     $('.graph').qtip({ 
      content: { 
       text: 'Loading...', // The text to use whilst the AJAX request is loading 
       ajax: { 
        url: '/foo/' + bar , // URL to the local file 
        type: 'GET', // POST or GET 
        data: {} // Data to pass along with your request 
       } 
      }, 
      show: { 
       solo: "true", 
       delay: 100, 
       event: "click", 
       adjust : {screen : true} 
      }, 
     }); 
    } 
}); 

此代碼的偉大工程,但只有當我刪除

function doStuff(bar) 
{ 
} 

,我想換的原因在一個函數中的東西是我可以傳入一個參數。就像我說的,我可以使用jQuery,但我遠離主人。歡迎任何對我的策略的幫助或建議/批評。

編輯

這就是我所說的功能。感謝所有的幫助!

<a class="graph" title="a_link_title" href="#" onclick="doStuff('281'); return false;"> a link </a> 
+3

反向工程的jQuery? WTF男人! –

+0

文檔就緒函數不提供任何參數,您希望傳遞給您的方法的是什麼? – Clayton

+0

@Pablo Fernandez我的意思是從已經寫好的代碼開始,找出發生了什麼並且模仿它 –

回答

3

第一件事

這個功能不應該是裏面的document.ready

function doStuff(bar) 
{ 

} 

因爲htis函數的代碼不執行,除非你調用這個函數不工作。

你可以做兩件事。調用函數或刪除功能,並搬出的document.ready和內部調用它的document.ready

$(document).ready(function(){ 
    doStuff(bar) // pass the value and call it like this 
}); 

    function doStuff(bar) 
    { 
     // Match all <A/> links with a title tag and use it as the content (default). 
     $('.graph').qtip({ 
      content: { 
       text: 'Loading...', // The text to use whilst the AJAX request is loading 
       ajax: { 
        url: '/foo/' + bar , // URL to the local file 
        type: 'GET', // POST or GET 
        data: {} // Data to pass along with your request 
       } 
      }, 
      show: { 
       solo: "true", 
       delay: 100, 
       event: "click", 
       adjust : {screen : true} 
      }, 
     }); 
    } 
+0

爲什麼不準備好它?它減少了全球污染。 – user113716

+0

@patrick,在他的情況下,他沒有調用該函數,可能這就是他的剩餘代碼沒有執行的原因。 – kobe

+0

是的,你說得對,他需要調用這個函數,但是不管它是否在ready回調中都不會有什麼區別,除非他也試圖從不同的變量環境中調用它。 – user113716

0

快速修復:

$(document).ready(function() { 
    function doStuff(bar) { 
     // your code 
    } 

    doStuff(123); // might help to actually call your function! :-) 

});