[ jQuery ] jquery 自定义插件!

来源:互联网 发布:白云先生新浪博客知乎 编辑:程序博客网 时间:2024/05/01 11:28

转载请自觉注明原文:http://www.jq-school.com/Show.aspx?id=169

jquery学堂是一个很不错的地方。

如果想要自己学一些jquery的插件。而不想去每次都写那些复杂的函数。那么下面的方法可以帮助你。

下面就自己写一个jquery.yangzhi.js.

$(function(){$.fn.yangzhi = function(options){var defaults = {Event:"click",msg:"Hello,world"};var options = $.extend(defaults,options);var $this = $(this);$this.live(options.Event,function(e){alert(options.msg);});}});

然后在html页面中调用就可以了。这里的一些属性都是用配置来解决的。

html页面如下:

<html><head><title>jquery.yangzhi.js test</title></head><script src = "jquery-1.7.2.js" type = "text/javascript"></script><script src = "jquery.yangzhi.js" type = "text/javascript"></script><script type = "text/javascript">$(function(){$("#test").yangzhi({Event:"click",msg:"so easy"});});</script><body><input type = "button" value = "login" id = "test"/></body></html>