标签:
(function(factory) { if (typeof define === ‘function‘ && define.amd) { // AMD define([‘jquery‘], factory); } else if (typeof module === ‘object‘ && module.exports) { factory(require(‘jquery‘)); } else { // Browser globals factory(jQuery); } }(function ($, undefined) { $.fn.ajaxdisplay = function (data, options) { var tpl,count = 0, $tag,$this = this,dataobj; var defaults = { template: $this.html(), oddClass: ‘‘, evenClass: ‘‘, }; try { if (typeof data == "object") { dataobj = data; } else { dataobj = eval(‘(‘ + data + ‘)‘); } } catch (err) { console.log(err); return $this; } settings = $.extend({}, defaults, options); tpl = settings.template; $this.html(‘‘); for (var x in dataobj) { $tag = $(TplReplace(tpl, dataobj[x])); $tag.addClass(count % 2 == 0 ? settings.evenClass : settings.evenClass); $this.append($tag); count++; } } function TplReplace(tpl,obj) { if(tpl.trim()!=‘‘){ return tpl.replace(/\{([\w\.]*)\}/g,function(str,key){ var keys = key.split(‘.‘),v = obj[keys.shift()]; for(var x in keys){ v = v[keys[x]]; } return (typeof v !== "undefined" && v !== null) ? v : ""; }); } else { var tagstr = ‘<tr>‘; for (var x in obj) { tagstr += ‘<td>‘ + obj[x] + ‘</td>‘; } tagstr += ‘</tr>‘; return tagstr; } } }));
标签:
原文地址:http://www.cnblogs.com/luowenxing/p/4651685.html