热门关键字:
jquery > jquery教程 > jquery教程 > 实现网页动态加载js和css文件的jquery扩展

实现网页动态加载js和css文件的jquery扩展

2961
作者:管理员
发布时间:2012/7/26 20:53:53
评论数:1
转载请自觉注明原文:http://www.jq-school.com/Show.aspx?id=17
 
 
此扩展可以更好的隐藏好自己写的JS和CSS文件,代码原理很简单,通俗易懂,话不多说看主要代码展示吧:
 
扩展代码如下:
  1. $.extend({   
  2.      includePath: '',   
  3.      include: function(file) {   
  4.         var files = typeof file == "string" ? [file]:file;   
  5.         for (var i = 0; i < files.length; i++) {   
  6.             var name = files[i].replace(/^\s|\s$/g, "");   
  7.             var att = name.split('.');   
  8.             var ext = att[att.length - 1].toLowerCase();   
  9.             var isCSS = ext == "css";   
  10.             var tag = isCSS ? "link" : "script";   
  11.             var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";   
  12.             var link = (isCSS ? "href" : "src") + "='" + $.includePath + name + "'";   
  13.             if ($(tag + "[" + link + "]").length == 0) document.write("<" + tag + attr + link + "></" + tag + ">");   
  14.         }   
  15.    }   
  16. });  
 
页面使用代码如下:
  1. $.includePath = 'http://www.jq-school.com/js/';    
  2. $.include(['manhua_date.js''jquery.lazyload.js''style.css']);  
 




如果您觉得本文的内容对您的学习有所帮助:支付鼓励



关键字:实现网页动态加载js和css文件的jquery扩展
友荐云推荐