Javascript jQuery 返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2438685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:20:08  来源:igfitidea点击:

Jquery return value

javascriptjquery

提问by Anton

I used a code:

我用了一个代码:

jQuery.fn.MyFunction = function(){
return this.each(function() {
    attributes = "test";

    return attributes;
});}

But when I call

但是当我打电话

 var1 = $(this).MyFunction();alert(var1);

I got an [object], but not the "test".

我有一个[对象],但不是“测试”。

How to allow jquery plugin return a some value?

如何允许 jquery 插件返回某个值?

采纳答案by Justin Ethier

jQuery plugins are generally designed to return a jQuery object so you can chain method calls:

jQuery 插件通常设计为返回一个 jQuery 对象,以便您可以链接方法调用:

jQuery("test").method1().method2() ...

If you want to return something else, use the following syntax:

如果要返回其他内容,请使用以下语法:


jQuery.fn.extend({
    myFunction: function( args ) {
            attributes = "test";

            return attributes;
    }
});

, or access it via its index using [].

,或使用[].

回答by Crozin

Here is once again your code:

这是您的代码:

jQuery.fn.MyFunction = function() { #1
   return this.each(function() {    #2
      return "abc";                 #3
   });                              #4
};                                  #5

Now let's check what every line do.

现在让我们检查每一行的作用。

  1. We declare property MyFunctionwhich is a function for every jQuery object.
  2. This line is first and the last statement of jQuery.MyFunction(). We returnthe result of this.each(), not the result of lambda-function (used as a argument for jQuery.each()). And this.each()returns itself so the final result is that you get jQuery object returned.
  1. 我们MyFunction为每个 jQuery 对象声明一个函数属性。
  2. 这一行是 的第一个也是最后一个语句jQuery.MyFunction()。我们返回的结果this.each(),而不是 lambda 函数的结果(用作 的参数jQuery.each())。并this.each()返回自身,因此最终结果是返回 jQuery 对象。

Lines 3-5 are not important in fact.

第 3-5 行实际上并不重要。

Just consider those two examples:

只考虑这两个例子:

jQuery.fn.MyFunction = function() {
    return this.each(function() {
        return "abc";
    });
};

jQuery.fn.AnotherFunction = function() {
    return "Hello World";
};

var MyFunctionResult = $(document).MyFunction();
var AnotherFunctionResult = $(document).AnotherFunction();

alert(MyFunctionResult);
alert(AnotherFunctionResult);

回答by Machiel

I believe jQuery returns the object, so you can maintain the chainability of diffrent functions.

我相信 jQuery 会返回对象,因此您可以保持不同函数的可链接性。

回答by mnemosyn

Hmm, perhaps use

嗯,也许用

var1 = $(this)[0].MyFunction();alert(var1);

But I'm not sure if that is what you want or if your code works at all. What are you trying to achieve? Are you sure you want to call this.each()?

但我不确定这是否是您想要的,或者您的代码是否有效。你想达到什么目的?你确定要打电话this.each()吗?

Like the others said, jQuery returns jQuery objects in most cases, and accessing the actual object can be accomplished using an indexer []or the getmethod.

就像其他人说的,jQuery 在大多数情况下返回 jQuery 对象,并且可以使用索引器[]get方法来访问实际对象。