为什么jquery插件函数总是返回对象而不是字符串?

发布于 2024-12-26 05:02:37 字数 579 浏览 2 评论 0 原文

这是我的自定义 jquery 插件的代码:

(function($){
    $.fn.extend({
        getmyValue : function(){
        return this.each(function() {
                return this.myVal;
        });
        },
        initPlugin : function(){
        return this.each(function() {
                this.myVal='Some results';
        });
        }
    });
})(jQuery);

当我运行此代码时:

$(document).ready(function() {

$("#div").initPlugin();
alert($("#div").getmyValue());
});

返回的值不是想象中的纯字符串,而是返回一个对象( $("#div") ),

我无法弄清楚为什么返回链接不起作用?

here is my code for a custom jquery plugin :

(function($){
    $.fn.extend({
        getmyValue : function(){
        return this.each(function() {
                return this.myVal;
        });
        },
        initPlugin : function(){
        return this.each(function() {
                this.myVal='Some results';
        });
        }
    });
})(jQuery);

when i run this code :

$(document).ready(function() {

$("#div").initPlugin();
alert($("#div").getmyValue());
});

the returned value is not a plain string as supposed but an object ( $("#div") is returned )

what i can't figure out is why the return chaining is not working ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜无邪 2025-01-02 05:02:37

因为 each 的返回值是您调用 each 的对象。函数eachcalls的返回值用于判断是否停止循环(即迭代函数可以返回false来停止循环) ;— 文档链接)。

从您的代码中不清楚您真正想在 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 从中获取原始数组。

Because the return value of each is the object on which you called each. The return value of the function each calls is used to determine whether to stop looping (that is, the iteration function can return false to stop looping — docs link).

It's unclear from your code what you really want to do in getmyValue; return a value you've stored on the jQuery instance itself? Return the myVal stored on the first contained element? Return an array of the myVal values from all the contained elements?

If you meant a myVal stored on the jQuery instance by your plugin:

getmyValue : function(){
    // Here, `this` is the jQuery instance on which `getmyvalue` was called
    return this.myVal;
},

If you meant the myVal on the first element (note that it's a raw DOM element in the typical case):

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;
},

If you meant an array of the myVal values from all the matched elements (again, these will be raw DOM elements in the typical case):

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();
},

...which uses map to get a jQuery-wrapped array of the values, and then get to get the raw array from it.

夏花。依旧 2025-01-02 05:02:37

您返回的是 this.each() 的结果,而不是 this.myVal

getmyValue: function() {
    return this.myVal;
}

You're returning the result of this.each() rather than this.myVal:

getmyValue: function() {
    return this.myVal;
}
℡Ms空城旧梦 2025-01-02 05:02:37

.each 的返回是一个对象。如果将其替换为 .map,那么您的代码将返回以逗号分隔的值列表。

jQuery 地图

The return of the .each is an object. If you replace that with .map then your code will return a comma delimited list of values.

jQuery Map

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文