一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

arttemplate的jQuery帮助方法

时间:2016-08-20 编辑:简简单单 来源:一聚教程网


需支持es5,兼容可以使用es5-shim

/**
 * 封装模板使用方法
 * @param  {string} tplName 模板名
 * @param  {[type]} data 传入模板的数据
 * @param  {[type]} method dom操作方法,默认使用html()方法
 * @return {[type]} 容器jQuery对象
 */
$.fn.tpl = function (tplName, data, method) {
  data = data || {};
  method = method || 'html';
  return this[method](template(tplName, data));
};
['append', 'prepend', 'after', 'before'].map(function (method) {
  (function (method) {
    $.fn[method + 'Tpl'] = function (tplName, data) {
      return this.tpl(tplName, data, method);
    }
  })(method);
});

热门栏目