因爲each
的返回值是您稱爲each
的對象。函數返回值each
調用用於確定是否停止循環(也就是說,迭代函數可以返回false
來停止循環。) — docs link)。
從代碼中不清楚你真的想在getmyValue
中做什麼;返回你存儲在jQuery實例本身的值?返回存儲在第一個包含元素上的myVal
?從所有包含的元素中返回myVal
值的數組?
如果你的意思是你的插件存儲在jQuery的實例myVal
:
getmyValue : function(){
// Here, `this` is the jQuery instance on which `getmyvalue` was called
return this.myVal;
},
如果你的第一個元素意味着myVal
(注意,這是在典型情況下,原始DOM元素):
getmyValue : function(){
// Here, `this` is the jQuery instance on which `getmyvalue` was called.
// `this[0]` is the first matched element (a raw DOM element, typically).
// Note we check before accessing it to see if it's there, since a jQuery
// instance may have matched zero elements.
return this[0] ? this[0].myVal : undefined;
},
如果指由所有匹配的元素的myVal
值的陣列(再次,這些將在典型的情況下,原料的DOM元素):
getmyValue : function(){
// Here, `this` is the jQuery instance on which `getmyvalue` was called.
return this.map(function() {
// Here, though, `this` one of the elements wrapped by the jQuery,
// instance typically a raw DOM element. (Each of them in a loop.)
return this.myVal;
}).get();
},
...它使用map
來獲取jQuery包裝的值的數組,然後get
從它得到原始數組。
你回來的'this.each'的結果,這不是一個字符串。你爲什麼不返回'this.myVal'?目前還不清楚你想要完成什麼。 – 2012-01-11 15:10:43