2011-01-30 168 views

回答

3

你只需要調用foo(),而不是el.foo()。 (除非我錯過了關於如何使用這個的東西。)

2

因爲foo不是el的財產。這是一個變量。

您將需要:

var foo = function() { 
    //... 
}; 

var boo = function() { 
    //... 
    foo(); 
} 

,如果你有它的工作:

var el = {}; 
el.foo = function() { 
    // ... 
}; 

var boo = function() { 
    //... 
    el.foo(); 
} 
2

在這種情況下,它看起來像foo()是不是對象el的屬性。你定義了這個函數,但是從所示的例子來看,它可能是一個全局函數。如果你想foo()對變量el工作,然後將它傳遞給函數,就像這樣:

var foo = function(element) { 
    //do something with this element 
    }; 

var boo = function() { 
    ... 
    foo(el); //pass el into the foo function so it can work on it. 
} 
3

因爲foo是沒有定義/連接到EL因此你不能調用從EL的上下文foo的功能。 你需要直接調用foo。

var foo = function() { 
    ... 
    }; 

    var boo = function() { 
    ... 
    foo(); 
    } 

但是如果你需要調用foo連接到EL的上下文那就試試這個:

var boo = function() { 
     ... 
     foo.call(el);//This calls foo in el's context 
     } 
3

如果我理解正確的話,你需要創建一個jQuery插件功能:

jQuery.fn.foo = function(){ 
    return this.each(function(){ 
     // element-specific code here 
    }); 
}; 

var boo = function() { 
    ... 
    el.foo(); 
} 
1

我假設,如果您要撥打:

el.foo() 

然後EL是jQuery對象,像

var el = $("#smth"); 
el.foo(); 

在這種情況下,你需要定義「富」爲:

$.fn.foo = function() { 
.... 
} 

否則,如果你只是想打電話從「富」'噓',然後就叫它沒有'el':

var foo = function() { 
... 
}; 

var boo = function() { 
    ... 
    foo(); 
} 

希望,它有幫助。