How to dynamically remove a stylesheet from the current page

后端 未结 9 2117
无人共我
无人共我 2020-12-04 12:17

Is there a way to dynamically remove the current stylesheet from the page?

For example, if a page contains:



        
9条回答
  •  一生所求
    2020-12-04 12:50

    Well, assuming you can target it with jQuery it should be just as simple as calling remove() on the element:

    $('link[rel=stylesheet]').remove();
    

    That will remove all external stylesheets on the page. If you know part of the url then you can remove just the one you're looking for:

    $('link[rel=stylesheet][href~="foo.com"]').remove();
    

    And in Javascript

    this is an example of remove all with query selector and foreach array

    Array.prototype.forEach.call(document.querySelectorAll('link[rel=stylesheet]'), function(element){
          try{
            element.parentNode.removeChild(element);
          }catch(err){}
        });
    
    //or this is similar
    var elements = document.querySelectorAll('link[rel=stylesheet]');
    for(var i=0;i

提交回复
热议问题