如何删除特定窗口屏幕宽度上的jquery / javascript?

时间:2014-04-07 10:52:33

标签: javascript jquery

我想知道这是他们以特定媒体屏幕大小删除外部JavaScript的任何方式。

我试过了

if($(window).width()>786){
    $("#idofscript").remove();
    // and $('#idofscript').removeattr('src');
    // and $('script[src="js/crawler.js"]').remove();
}

但它无法正常工作。

1 个答案:

答案 0 :(得分:0)

试试这个:

<强>代码:

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()
}}

<强>用法:

removejscssfile("somescript.js", "js") //remove all occurences of "somescript.js" on page
removejscssfile("somestyle.css", "css") //remove all occurences "somestyle.css" on page

<强> Reference