开发者

Create jquery plugins

开发者 https://www.devze.com 2023-01-27 03:39 出处:网络
Hello I am writing some jquery plugins, i read some tutorials finding all clear. But I can\'t find a tutorial that explains how to add events (better say call functions), for example I want to call a

Hello I am writing some jquery plugins, i read some tutorials finding all clear. But I can't find a tutorial that explains how to add events (better say call functions), for example I want to call a function on myplugin open event. I want to do something similar to the jquery dialog events, just like something the open event:

$('#adialog').dialog({
  open:function()
  {
    //I know this will be called on open of a dialog, how to do in my plugin?
  }
});

.

So what i want to have is this:

$('#somediv').myplugin({
  onOpen:function()// this is not clear how to do it in plugin
  {
     //do stuff here
  },
  background-color:'red',//this is clear, $.extends defaults options
  text:'blablabla',// this is clear $.extends defaults options
  .........
});

I want simply to know how to add "events" to my plugin, where "events" stand for events methods of a plugin, just for example in http://jqueryui.com/demos/draggab开发者_JAVA技巧le/#events the Event tab has events who can take some functions.


Callback functions are indistinguishable from normal functions and can be called like any other method.

However, if the callback is optional, you should check that it exists before calling it to avoid runtime errors.

For example:

$.fn.myPlugin = function(options) {
    if (options.onOpen)     //If the callback was passed,
        options.onOpen(...);
});
0

精彩评论

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

关注公众号