Last active
December 17, 2015 21:39
-
-
Save PizzaLiu/5675854 to your computer and use it in GitHub Desktop.
js动态载入css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function addCss(cssPath){ | |
var newCss=document.createElement("link"); | |
newCss.setAttribute("rel", "stylesheet"); | |
newCss.setAttribute("type", "text/css"); | |
newCss.setAttribute("href", cssPath); | |
document.getElementsByTagName("head")[0].appendChild(newCss); | |
} | |
function removeCss(cssName){ | |
var link=document.getElementsByTagName("link"); | |
for(var i=0;i<link.length;i++){ | |
if(null!=link[i].getAttribute("href") && link[i].getAttribute("href").indexOf(cssName)!=-1 && i!=0){//存在重复 只保留第一个link | |
link[i].parentNode.removeChild(link[i]); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment