我用了一个代码:
jQuery.fn.MyFunction = function(){
return this.each(function() {
attributes = "test";
return attributes;
});}
但是当我调用
var1 = $(this).MyFunction();alert(var1);
我得到了一个[对象],但不是“测试”。
如何让jquery插件返回一个值?
最佳答案
jQuery 插件通常设计为返回一个 jQuery 对象,因此您可以链接方法调用:
jQuery("test").method1().method2() ...
如果要返回其他内容,请使用以下语法:
jQuery.fn.extend({
myFunction: function( args ) {
attributes = "test";
return attributes;
}
});
,或使用 []
通过其索引访问它。
关于javascript - Jquery 返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2438685/