作者博客:http://blog.csdn.net/stationxp 作者微博:http://weibo.com/liuhailong2008 转载请取得作者允许回正题。
<p id='p'></p> $('#p').hailongjsnote();支持属性:sort——asc正序,desc倒叙。
形如:
<p id='p' sort='asc'></p> $('#p').hailongjsnote('option','sort','desc'); var cnt = $('#p').hailongjsnote('getCount'); <p id='p' preload='func();'></p> $('#p').hailongjsnote('on','loaded',function(event,notes){ // });
/** * hailongjsnote * author : liuhailong * date : 2014-10-28 * Dependencies: * jquery.mini.js / jquery.js * jquery.parser.js */ (function($){ function init(target){ $(target).addClass('hailongjsnote'); $(target).html('<ul class="jsnote-list"></ul>'); return $(target); } function setContent(target,notes){ var item , htm = ''; for(var idx = 0; idx < notes.length; ++idx){ item = notes[idx]; htm += '<li><a href="'+item.href+'">'+item.title+'</a></li>'; } $(target).find('ul').html(htm); } $.fn.hailongjsnote = function(options, param){ if (typeof options == 'string'){ var method = $.fn.hailongjsnote.methods[options]; if (method){ return method(this, param); } } options = options || {}; return this.each(function(){ var state = $.data(this, 'hailongjsnote'); if (state){ $.extend(state.options, options); } else { state = $.data(this, 'hailongjsnote', { options: $.extend({}, $.fn.hailongjsnote.defaults, $.fn.hailongjsnote.parseOptions(this), options), jsnote: init(this), notes : $.fn.hailongjsnote.getNotes() }); } setContent(this,state.notes); }); }; $.fn.hailongjsnote.parseOptions = function(target){ return $.extend({}, $.parser.parseOptions(target, ['width','height','sort','click'])); }; $.fn.hailongjsnote.getNotes = function(){ return [ { sn:1, title:'Java程序猿的JavaScript学习笔记(1——理念) ', href:'http://blog.csdn.net/stationxp/article/details/40020009' },{ sn:2, title:'Java程序猿的JavaScript学习笔记(2——属性复制和继承) ', href:'http://blog.csdn.net/stationxp/article/details/40068345' },{ sn:3, title:'Java程序猿的JavaScript学习笔记(3——this/call/apply) ', href:'http://blog.csdn.net/stationxp/article/details/40130687' },{ sn:4, title:'Java程序猿的JavaScript学习笔记(4——this/闭包/getter/setter) ', href:'http://blog.csdn.net/stationxp/article/details/40146441' },{ sn:5, title:'Java程序猿的JavaScript学习笔记(5——prototype) ', href:'http://blog.csdn.net/stationxp/article/details/40205967' },{ sn:6, title:'Java程序猿的JavaScript学习笔记(6——面向对象模拟) ', href:'http://blog.csdn.net/stationxp/article/details/40264845' },{ sn:7, title:'Java程序猿的JavaScript学习笔记(7——jQuery基本机制) ', href:'http://blog.csdn.net/stationxp/article/details/40384477' },{ sn:8, title:'Java程序猿的JavaScript学习笔记(8——jQuery选择器) ', href:'http://blog.csdn.net/stationxp/article/details/40476959' },{ sn:9, title:'Java程序猿的JavaScript学习笔记(9——jQuery工具方法) ', href:'http://blog.csdn.net/stationxp/article/details/40480185' },{ sn:10, title:'Java程序猿的JavaScript学习笔记(10——jQuery-在“类”层面扩展) ', href:'http://blog.csdn.net/stationxp/article/details/40492735' },{ sn:11, title:'Java程序猿的JavaScript学习笔记(11——jQuery-在“对象”层面扩展) ', href:'http://blog.csdn.net/stationxp/article/details/40497837' },{ sn:12, title:'Java程序猿的JavaScript学习笔记(12——jQuery-扩展选择器) ', href:'http://blog.csdn.net/stationxp/article/details/40501123' },{ sn:13, title:'Java程序猿的JavaScript学习笔记(13——jQuery UI) ', href:'http://blog.csdn.net/stationxp/article/details/40534209' },{ sn:14, title:'Java程序猿的JavaScript学习笔记(14——扩展jQuery UI) ', href:'http://blog.csdn.net/stationxp/article/details/40535073' } ]; } $.fn.hailongjsnote.defaults = { width: '500px' }; })(jQuery);
function setContent(target,notes){ var item , htm = ''; var opts = $.data(target, 'hailongjsnote').options;//opts里存着各种參数 if('desc' == opts.sort){ for(var idx = notes.length-1; idx >=0; --idx){ item = notes[idx]; htm += '<li><a href="'+item.href+'">'+item.title+'</a></li>'; } }else{ for(var idx = 0; idx < notes.length; ++idx){ item = notes[idx]; htm += '<li><a href="'+item.href+'">'+item.title+'</a></li>'; } } $(target).find('ul').html(htm); }
$.fn.hailongjsnote = function(options, param,param2){ if (typeof options == 'string'){ var method = $.fn.hailongjsnote.methods[options]; if (method){ return method(this, param,param2); } }添加例如以下代码实现:
$.fn.hailongjsnote.methods = { //$('#p').hailongjsnote('option','sort','desc'); option: function(jq,opt,param){ var state = $.data(jq[0], 'hailongjsnote'); var opts = state.options; if(param === undefined ){//没有传入第三个參数 if(opt){ // 全部的属性都能够读取 return opts[opt]; } }else{ // 给属性设值,不是每一个属性都同意 if(opt){ opts[opt] = param; // 缓存的值也自己主动更新了吧? if(opt=='sort'){ setContent(jq,state.notes); } } } } };
$.fn.hailongjsnote.methods = { getCount : function(jq){ var state = $.data(jq[0], 'hailongjsnote'); var notes = state.notes; return notes && notes.length ?notes.length : 0; } };
// 下面代码未经调试 function setContent(target,notes){ var opts = $.data(target, 'hailongjsnote').options; var preload = opts['preload']; var loaded = opts['loaded']; notes = preload && preload(notes); var item , htm = ''; ... $(target).find('ul').html(htm); loaded && loaded(htm); }
版权声明:本文博客原创文章。博客,未经同意,不得转载。