简体   繁体   English

编写一个接受参数和对象的jQuery函数

[英]write a jquery function which accepts params and object

This should be an easy one. 这应该很容易。

How to write a jquery function that gets the 'calling' object and some params. 如何编写一个获取“调用”对象和一些参数的jQuery函数。

My function code: 我的功能代码:

new function($) {
  $.fn.addcolor = function(param1, param2, param3, param4) {
    // here i would like to retrieve the 'caller' object (div.foo in this case)

  }
} (jQuery);

My code that 'calls' the function: 我的代码“调用”该函数:

$('div.foo').click(function() {
  $(this).addcolor(1, 2, 3, 4);
});

I can get the params no problemo, but I want to get the content of the div.foo in the function and add some content it. 我可以毫无问题地获取参数,但是我想在函数中获取div.foo的内容并添加一些内容。

You're looking for this . 您正在寻找this

jQuery plugins in $.fn are normal methods on the $ prototype. $.fn中的jQuery插件是$原型上的常规方法。 (jQuery assigns $.fn to $.prototype ) (jQuery将$.fn分配给$.prototype
Like any other method, you can get the context in which it was invoked using this . 与其他任何方法一样,您可以使用this获得在其中调用它的上下文。

In your addcolor plugin, this will represent the same jQuery object against which your plugin was called. 在您的addcolor插件中, this将代表与您的插件相同的jQuery对象。

 // v---jQuery object
$(this).addcolor(1, 2, 3, 4);

(function($) {
  $.fn.addcolor = function(param1, param2, param3, param4) {
    // here "this" is the same object
    // this[ 0 ] to get the first DOM element
    // this.each(function(...   to iterate over the collection
  }
})(jQuery);

In jQuery plugin functions that element is referenced by this. 在jQuery插件函数中,此元素引用此元素。

function($) {
  $.fn.addcolor = function(param1, param2, param3, param4) {
    // here i would like to retrieve the 'caller' object (div.foo in this case)
    var divcontent = $(this).html();
  }
} (jQuery);

this.whatever is one way... 这是一种方法...

There is also an arguments object that you can call. 您还可以调用一个arguments对象。 ie: arguments.length or arguments["parm"] 即:arguments.length或arguments [“ parm”]

To extend jQuery I used: 为了扩展jQuery,我使用了:

(function($) {
    $.fn.extend({
        addcolor: function(param1, param2, param3, param4) {
            return this.empty().append(something);
        }
    });
})(jQuery);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编写一个 function 称为键,它接受 object 并返回 object 中所有键的数组 - Write a function called keys, which accepts an object and returns an array of all of the keys in the object 如何推送将承诺返回给数组的函数,该数组接受参数 - How to push a function that returns a promise to an array, which accepts params 在 javascript 中编写一个接受任意数量的 object 的 function - write a function that accepts any number of object in javascript 如何动态确保对象中的函数仅使用它接受的参数调用? - How to dynamically ensure that a function in an object is called only with the params that it accepts? Write a function called values, which accepts an object and returns an array of all of the values in the object w/o using Object.values() - Write a function called values, which accepts an object and returns an array of all of the values in the object w/o using Object.values() 如何编写一个接受回调函数的jQuery插件方法? - How do I write a jQuery plugin method that accepts a callback function? 如何编写一个接受回调作为参数的jquery函数 - How do I write a jquery function that accepts a callback as a parameter 如何使用可传递Params的回调编写jQuery函数? - How to write a jQuery function with a callback that you can pass Params to? 编写一个域频率“分类”function,它接受 URL 数组并返回预期的 output - Write a Domain frequency 'categorize' function which accepts array of URLS and returns expected output 编写一个接受字符串的函数,并返回一个只传递给字符串的大写字母的新字符串 - write a function which accepts a string and returns a new string with only the capital letters passed to the string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM