javascript - 动态删除外部js文件

标签 javascript jquery html

我正在尝试删除鼠标单击时的 js 文件。我指的是 www.javascriptkit.com/javatutors/loadjavascriptcss2.shtml 来删除 但它不适合我 这是我对应的代码

HTML 部分:

<div id="dropdownlist" style="position:absolute;z-index:102;">
                  <a href="#" onclick="DisplayDropdownSelectedPage('blog')">BLOG</a><br/><a href="#" onclick="DisplayDropdownSelectedPage('careers')">CAREERS</a><br/><a href="#" onclick="DisplayDropdownSelectedPage('whistleblower')">WHISTLE BLOWSER</a><br/><a href="#" onclick="DisplayDropdownSelectedPage('disclaimer')">DISCLAIMER</a><br/><a href="#" onclick="DisplayDropdownSelectedPage('sitemap')">SITEMAP</a><br/><a href="#" onclick="DisplayDropdownSelectedPage('feedback')">FEEDBACK</a><br/>
                </div>

JS部分:

function removejscssfile(filename, filetype){
 var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist from
 var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
 var allsuspects=document.getElementsByTagName(targetelement)
 for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
  if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(filename)!=-1)
   allsuspects[i].parentNode.removeChild(allsuspects[i]) //remove element by calling parentNode.removeChild()
 }
}

function DisplayDropdownSelectedPage(dropdownname)
{
    var width=$(window).width();
    var height=$(window).height();
    var top=document.getElementById("globelHead").offsetHeight;
    var animateheight=(height-top);
    var animatewidth=(width/2);
    var shiftright=(-(animatewidth)+"px");
    if(dropdownname=="blog")
    {
        removejscssfile("myscript.js", "js")
        $("#Blog").css({"width":animatewidth+"px","height":animateheight,"right":shiftright,"top":top});
        $("#Blog").show().animate({right:'0%'},1200);
        $("#Careers").hide();
        $("#WhistleBlower").hide();
        $("#Disclaimer").hide();
        $("#SiteMap").hide();
        $("#Feedback").hide();

    }
    else if(dropdownname=="careers")
    {
        removejscssfile("myscript.js", "js")
        $("#Blog").hide();
        $("#Careers").css({"width":animatewidth+"px","height":animateheight,"right":shiftright,"top":top});
        $("#Careers").show().animate({right:'0%'},1200);
        $("#WhistleBlower").hide();
        $("#Disclaimer").hide();
        $("#SiteMap").hide();
        $("#Feedback").hide();
    }
    else if(dropdownname=="whistleblower")
    {
        removejscssfile("myscript.js", "js")
        $("#Blog").hide();
        $("#Careers").hide();
        $("#WhistleBlower").css({"width":animatewidth+"px","height":animateheight,"right":shiftright,"top":top});
        $("#WhistleBlower").show().animate({right:'0%'},1200);
        $("#Disclaimer").hide();
        $("#SiteMap").hide();
        $("#Feedback").hide();
    }
    else if(dropdownname=="disclaimer")
    {   
        removejscssfile("myscript.js", "js")
        $("#Blog").hide();
        $("#Careers").hide();
        $("#WhistleBlower").hide();
        $("#Disclaimer").css({"width":animatewidth+"px","height":animateheight,"right":shiftright,"top":top});
        $("#Disclaimer").show().animate({right:'0%'},1200);
        $("#SiteMap").hide();
        $("#Feedback").hide();

    }
    else if(dropdownname=="sitemap")
    {

        $("#Blog").hide();
        $("#Careers").hide();
        $("#WhistleBlower").hide();
        $("#Disclaimer").hide();
        $("#SiteMap").css({"width":animatewidth+"px","height":animateheight,"right":shiftright,"top":top});
        $("#SiteMap").show().animate({right:'0%'},1200);
        $("#Feedback").hide();

    }
    else if(dropdownname=="feedback")
    {
        $("#Blog").hide();
        $("#Careers").hide();
        $("#WhistleBlower").hide();
        $("#Disclaimer").hide();
        $("#SiteMap").hide();
        $("#Feedback").css({"width":animatewidth+"px","height":animateheight,"right":shiftright,"top":top});
        $("#Feedback").show().animate({right:'0%'},1200);
    }
}

最佳答案

不太明白你的问题,但如果你需要从 DOM 文档中删除文件,那么添加到脚本标签属性 id 并删除。

<script id="mustBeRemoved">....
$("#mustBeRemoved").remove();

关于javascript - 动态删除外部js文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23148541/

相关文章:

javascript - 添加类定义的不透明度不会影响 Webkit 中的元素

javascript - 从输入表单中删除 HTML TAG 输入

javascript - 如何使用 request.query 检索变量

javascript - 如果在小型设备上用错误文本替换 iframe

javascript - Bootstrap 月年选择器

jquery - 使用 Ajax 时 reCaptcha 不会重新加载

html - 如何避免表格在打印预览中中断?

javascript - 覆盖与父级溢出时对齐的 div

html - 调整屏幕大小时将内容推离侧边栏

jquery - 编写第一个 jQuery 插件 : understanding this