因为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