1.注意事项
- 在使用扩展组件的时候,页面中必须先引入jquery.js,再引入ui.js。顺序反了会导致组件出错。
2.类级别组件开发
Edit
对jquery方法的扩展主要以下两种模式
一、
(function($){ $.methodName=function(){ //things to be done }; })(jQuery); |
---|
二、
jQuery.extend({ methodName:function(){ //things to be done } }); |
---|
Example:
对jquery扩展一个test方法,能将页面中的div全部加上一个样式
background-color:green |
---|
可添加如下一个js文件,命名为ui.js:
(function($){ $.test=function(){ $('div').css('background-color','red') }; })(jQuery); |
---|
接下来只需要在页面中引入ui.js,并使用$.test()就可将页面中的div全部变成红色背景。
3.对象级别组件开发
Edit
对jquery对象方法的扩展主要以下两种模式:
一、
(function($){ $.fn.test=function(){ //things to be done }; })(jQuery); |
---|
二、
jQuery.extend({ test:function(){ //things to be done } }); |
---|