Home >Web Front-end >JS Tutorial >Detailed explanation of the steps to dynamically load Js files and Css files with jquery

Detailed explanation of the steps to dynamically load Js files and Css files with jquery

php中世界最好的语言
php中世界最好的语言Original
2018-04-23 13:46:561246browse

This time I will bring you a detailed explanation of the steps for jquery to dynamically load Js files and Css files. What are the precautions for jquery to dynamically load Js files and Css files. The following is a practical case, let's take a look.

If you have many related CSS files that you want to load together, or want to load different CSS files dynamically, then the following method will definitely be helpful to you.

Jquery dynamic loading Js and Css extension method

$.extend({ 
   includePath: '', 
   include: function(file) { 
    var files = typeof file == "string" ? [file]:file; 
    for (var i = 0; i < files.length; i++) { 
      var name = files[i].replace(/^\s|\s$/g, ""); 
      var att = name.split(&#39;.&#39;); 
      var ext = att[att.length - 1].toLowerCase(); 
      var isCSS = ext == "css"; 
      var tag = isCSS ? "link" : "script"; 
      var attr = isCSS ? " type=&#39;text/css&#39; rel=&#39;stylesheet&#39; " : " language=&#39;javascript&#39; type=&#39;text/javascript&#39; "; 
      var link = (isCSS ? "href" : "src") + "=&#39;" + $.includePath + name + "&#39;"; 
      if ($(tag + "[" + link + "]").length == 0) document.write("<" + tag + attr + link + "></" + tag + ">"); 
    } 
  } 
});

Usage:

$.include('http://www.jb51.net/include/dedeajax.js'); 
$.include('http://www.jb51.net/templets/skins/style/android.css');

Or:

$.includePath='www.jb51.net/html/'; 
$.include([dedeajax.js','android.css']);

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Using jquery plug-in ajaxupload for file upload

##jQuery plug-in Tocify dynamic node implementation directory menu

jQuery plugin limits upload file size and format usage

The above is the detailed content of Detailed explanation of the steps to dynamically load Js files and Css files with jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn