jQuery 一个流行的JS库。从工作到现在,接触最多的一个js库,有时候难免会用其他的第三方的插件,我自己也略懂一些,就此花了点时间写了点简单的实现jQuery插件的demo。
内容不多,可以直接复制代码保存成html文件在本地打开。
<!DOCTYPE html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript" >
(function($){
// 全局
$.denghb = function(){
console.log('denghb');
}
// 方法
$.fn.denghb = function(param){
$(this).append('fn denghb '+param);
}
// 扩展
$.extend({
ext_denghb:function(param){
console.log('extend denghb '+param);
}
});
$.fn.extend({
ext_fn_denghb:function(){
$(this).append('<br/>ext fn denghb');
}
});
//
$.fn.style = function(options){
var defaults = {
background:'#eee',
color:'red'
}
options = $.extend(defaults,options);
$(this).css(options);
}
// more ...
//console.log($);
})(jQuery);
$(function(){
$.denghb();
$('body').denghb('123');
$.ext_denghb(321);
$('body').ext_fn_denghb();
$('#adv').style({background:'#fff'});
});
</script>
</head>
<body>
<div id="adv" style="width:100px;height:100px;">
hello jquery plugin
</div>
</body>
</html>
动手后才发现世界上没有一件简单的事情
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名,转载请标明出处;