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
精彩评论