var gTag = 1; function clickButton() { if(gTag == 1) { //加载js /*var linkObj = document.createElement("script"); linkObj.setAttribute("src","b.js"); document.body.appendChild(linkObj); gTag = 2; removejscssfile("b.js", "js");*/ //加载css var linkObj = document.createElement("link"); linkObj.setAttribute("href","a.css"); linkObj.setAttribute("rel","stylesheet"); document.body.appendChild(linkObj); gTag = 2; removejscssfile("b.css", "css"); } else { //加载js /*var linkObj = document.createElement("script"); linkObj.setAttribute("src","c.js"); document.body.appendChild(linkObj); gTag = 1; removejscssfile("a.js", "js");*/ //加载css var linkObj = document.createElement("link"); linkObj.setAttribute("href","b.css"); linkObj.setAttribute("rel","stylesheet"); document.body.appendChild(linkObj); gTag = 1; removejscssfile("a.css", "css"); } } function removejscssfile(filename,type){ var targetelement; var targetattr if(type == ‘css‘) { targetelement = ‘link‘; targetattr = ‘href‘; } else { targetelement = ‘script‘; targetattr = ‘src‘; } var allsuspects=document.getElementsByTagName(targetelement); for (var i=allsuspects.length-1; i>=0; i--) { if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(filename)!=-1) allsuspects[i].parentNode.removeChild(allsuspects[i]); } }
时间: 2024-10-10 11:24:29