Skip to content

Instantly share code, notes, and snippets.

@PizzaLiu
Last active December 17, 2015 21:39
Show Gist options
  • Save PizzaLiu/5675854 to your computer and use it in GitHub Desktop.
Save PizzaLiu/5675854 to your computer and use it in GitHub Desktop.
js动态载入css
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