码迷,mamicode.com
首页 > Web开发 > 详细

动态添加删除css/js文件

时间:2015-07-01 12:08:20      阅读:338      评论:0      收藏:0      [点我收藏+]

标签:

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]);
    }
} 

 

动态添加删除css/js文件

标签:

原文地址:http://www.cnblogs.com/kongxf/p/4612729.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!