Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

javascript - 如何创建jQuery函数(新的jQuery方法或插件)?(How to create a jQuery function (a new jQuery method or plugin)?)

I know that in JavaScript the syntax is as follows:

(我知道JavaScript中的语法如下:)

function myfunction(param){
  //some code
}

Is there a way to declare a function in jQuery that can be added to an element?

(有没有一种方法可以在jQuery中声明可以添加到元素的函数?)

For example:

(例如:)

$('#my_div').myfunction()
  ask by multimediaxp translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From the Docs :

(从文档中 :)

(function( $ ){
   $.fn.myfunction = function() {
      alert('hello world');
      return this;
   }; 
})( jQuery );

Then you do

(那你做)

$('#my_div').myfunction();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...