jQuery動(dòng)態(tài)加載css文件實(shí)現(xiàn)方法
有時(shí)我們可能會(huì)需要使用 jQuery 來加載一個(gè)外部的 css 文件,如在切換頁面布局時(shí)。思路是創(chuàng)建一個(gè) link 元素,并將它添加到 標(biāo)記中即可,下邊首先看看怎么使用 jQuery 來實(shí)現(xiàn)。
下邊是我喜歡的寫法:
$("<link>") .attr({ rel: "stylesheet", type: "text/css", href: "site.css" }) .appendTo("head");
有些朋友可能會(huì)使用下邊的寫法,只是形式有些小差異(append appendTo),原理還是一樣的。
$("head").append("<link>"); css = $("head").children(":last"); css.attr({ rel: "stylesheet", type: "text/css", href: "/Content/Site.css" });
最后,有的朋友可能希望直接在 javascript 中使用,方法如下:
function addCSS() { var link = document.createElement('link'); link.type = 'text/css'; link.rel = 'stylesheet'; link.href = '/Content/Site.css'; document.getElementsByTagName("head")[0].appendChild(link); }
如果是在 web 交互時(shí),我們可以使用上述的方法通過 jQuery 或者 javascript 來引入一個(gè) css 文件,否則還是建議使用原始的方法。
下面我還介紹一個(gè)可加載js,css的實(shí)例
代碼如下
$.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('.'); var ext = att[att.length - 1].toLowerCase(); var isCSS = ext == "css"; var tag = isCSS ? "link" : "script"; var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' "; var link = (isCSS ? "href" : "src") + "='" + $.includePath + name + "'"; if ($(tag + "[" + link + "]").length == 0) document.write("<" + tag + attr + link + "></" + tag + ">"); } } }); //使用方法 $.includePath = 'http://hi.xxx/javascript/'; $.include(['json2.js', 'jquery.tree.js', 'jquery.tree.css']);
一個(gè)完整的實(shí)例
index.html
<!-- Created by Barrett at RRPowered.com --> <!-- File name index.html --> <html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax /libs/jquery/1.4.4/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="default.css"> <title>A simple jQuery image slide</title> <script type="text/javascript"> $(function(){ $(".theme").click(function(){ var theme=$(this).attr("rel"); $("link").attr("href",$(this).attr('rel')); $("head").append("<link>"); }); }); </script> </head> <body> <div class="theme" rel="blue.css">Blue</div> <div class="theme" rel="orange.css">Orange</div> <div class="theme" rel="yellow.css">Yellow</div> <div class="theme" rel="default.css">Default</div> <div class="container"> <div class="menu">Tab1 Tab2 Tab3 Tab4 Tab5</div> <div class="inner"> Lorem ipsum dolor sit amet </div> <div class="footer">copyright yoursite 2011</div> </div> </body> </html> default.css body{ background-color:#ffffff; font-family:"arial"; } .theme{ margin:10px; width:70px; padding:5px; text-align:center; background-color:#BEF781; border:solid #333333 1px; color:#444444; font-weight:bold; cursor:pointer; } .container{ margin-left:auto; margin-right:auto; width:700px; } .inner{ padding:20px; border:solid #333333 1px; } .menu{ background-color:#f2f2f2; padding:10px; font-weight:bold; } .footer{ background-color:#f9f9f9; padding:5px; } blue.css body{ background-color:#2E9AFE; font-family:"arial"; } .theme{ margin:10px; width:70px; padding:5px; text-align:center; background-color:#BEF781; border:solid #333333 1px; color:#444444; font-weight:bold; cursor:pointer; } .container{ margin-left:auto; margin-right:auto; width:700px; } .inner{ padding:20px; border:solid #333333 1px; background-color:#58ACFA; color:#ffffff; } .menu{ background-color:#f2f2f2; padding:10px; font-weight:bold; } .footer{ background-color:#f9f9f9; padding:5px; } yellow.css body{ background-color:#F7FE2E; font-family:"arial"; } .theme{ margin:10px; width:70px; padding:5px; text-align:center; background-color:#BEF781; border:solid #333333 1px; color:#444444; font-weight:bold; cursor:pointer; } .container{ margin-left:auto; margin-right:auto; width:700px; } .inner{ padding:20px; border:solid #333333 1px; background-color:#f6f6f6; } .menu{ background-color:#F2F5A9; padding:10px; font-weight:bold; } .footer{ background-color:#F2F5A9; padding:5px; } orange.css body{ background-color:#FE9A2E; font-family:"arial"; } .theme{ margin:10px; width:70px; padding:5px; text-align:center; background-color:#BEF781; border:solid #333333 1px; color:#444444; font-weight:bold; cursor:pointer; } .container{ margin-left:auto; margin-right:auto; width:700px; } .inner{ padding:20px; background-color:#F7BE81; color:#404040; } .menu{ background-color:#ffffff; padding:10px; font-weight:bold; color:#FFBF26; } .footer{ background-color:#ffffff; padding:5px; color:#FFBF26; }
相關(guān)文章
jQuery-1.9.1源碼分析系列(十)事件系統(tǒng)之事件包裝
這篇文章主要介紹了jQuery-1.9.1源碼分析系列(十)事件系統(tǒng)之事件包裝的相關(guān)資料,需要的朋友可以參考下2015-11-11jQuery實(shí)現(xiàn)滾動(dòng)到底部時(shí)自動(dòng)加載更多的方法示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)滾動(dòng)到底部時(shí)自動(dòng)加載更多的方法,涉及jQuery基于ajax動(dòng)態(tài)操作頁面元素相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-02-02前端html中jQuery實(shí)現(xiàn)對文本的搜索功能并把搜索相關(guān)內(nèi)容顯示出來
這篇文章主要介紹了前端html中jQuery實(shí)現(xiàn)對文本的搜索功能并把搜索相關(guān)內(nèi)容顯示出來,在項(xiàng)目中經(jīng)常會(huì)遇到,今天小編把實(shí)例代碼分享給大家,需要的朋友可以參考下2017-11-11jquery實(shí)現(xiàn)類似EasyUI的頁面布局可改變左右的寬度
這篇文章主要介紹了通過jquery實(shí)現(xiàn)類似EasyUI的頁面布局可改變左右的寬度,需要的朋友可以參考下2014-07-07jQuery右鍵菜單contextMenu使用實(shí)例
在最近項(xiàng)目中需要頻繁的右鍵菜單操作。我采用了contextMenu這款jQuery插件。2011-09-09淺談jQuery綁定事件會(huì)疊加的解決方法和心得總結(jié)
下面小編就為大家?guī)硪黄獪\談jQuery綁定事件會(huì)疊加的解決方法和心得總結(jié)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10