开发者

Question| Creating a jQuery plugin [closed]

开发者 https://www.devze.com 2023-03-31 22:34 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I am familiar with jQuery and I wanted to ask something correlating to a plugin/code that I am trying to create.

1) What kinds of plugins that I can create, except for the : $('element').myPlugin(); ? Can I create a plugin that creates a div? like so: myPlugin(); By using jQuery standards?

2) Let's say that I want to create a nice sticky div with jQuery and I want it to开发者_JAVA百科 be the easiest for the user to implement, how can I do so? (e.g. The user will only need to paste the function somewhere in the code after the DOM was ready).

Thanks in advance.


The basis of creating a plugin for jQuery is actually quite simple.

Following the jQuery plugin authoring guidelines is a good idea. Bear in mind that

1) plugins files should be named jquery.PLUGINNAME.js and

2) always attach your plugins to jQuery instead of $, so aliases can be easily set using the noConflict() method.

The structure required to extend the jQuery.fn object is

jQuery.fn.myFirstPlugin = function () {
   return this.each (function () {
     //Write your logic here

   });
}

This is how you will call it

$('#test').firstPlugin();


Below is the link of the simple tutorial on how to make a jquery plugin:

http://blog.jeremymartin.name/2008/02/building-your-first-jquery-plugin-that.html

0

精彩评论

暂无评论...
验证码 换一张
取 消