简体   繁体   English

jQuery-如何定义用户定义的jQuery函数?

[英]Jquery - How to defined user defined jquery function?

Can we create user defined function in jquery like : 我们可以在jquery中创建用户定义的函数,例如:

    $(selector).click(function()
   {

    })

Above function is already in jquery library. 上面的功能已经在jQuery库中。

if i want to create function like above one For eg : 如果我想创建一个以上功能,例如:

$(selector).demo();

How i can create function "demo()" to which i can prefix the selector as $(selector).demo(); 我如何创建函数“ demo()”,可以将选择器作为$(selector).demo()的前缀

So: 所以:

jQuery.fn.newfunction= function() {

//code goes here

};

And then call: 然后调用:

$(element).newfunction();

jQuery.fn.extend() jQuery.fn.extend()

by jQeury.fn.extend() 通过jQeury.fn.extend()

jQuery.fn.extend({
    demo: function(value){
       alert(value);
    }
});

you can also pass parameters if you need, like 10 in this example. 您还可以根据需要传递参数 ,例如本例中的10。

$(selector).demo(10);

if you would like to pass a function 如果您想传递功能

$.fn.extend({
        demo: function(func){
            if(typeof func === 'function')
                func();
        });

pass the function then you trigger it in demo 传递函数,然后在演示中触发它

  $(selector).demo(function(){
      alert('test');
  });

hope it will help you! 希望对您有帮助!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM