jQuery – enable/disable all style sheets on page on click

后端 未结 6 1178
情书的邮戳
情书的邮戳 2020-12-06 06:05

Is it possible to detect all style sheets on the current page with a click of a ‘disable/enable CSS’ button and disable them on the first click so none of the styling applie

相关标签:
6条回答
  • 2020-12-06 06:23

    Here is another method that you can try to do that thing.

    $('*[rel=stylesheet]').attr('disabled', 'true');
    $('link[rel=stylesheet]').attr('disabled', 'true');
    

    and in other hand you remove that attribute

    $('link[rel=stylesheet]').attr('disabled', 'false');
    $('*[rel=stylesheet]').attr('disabled', 'false');
    
    0 讨论(0)
  • 2020-12-06 06:33

    I found the found the following worked in all browsers for me:

    CSS Link:
    <link rel="stylesheet" href="http://basehold.it/22" data-role="baseline">
    
    JQuery:
    $('link[data-role="baseline"]').attr('href', '');
    

    The above will disable only that one stylesheet, and this could be toggled on and off.

    0 讨论(0)
  • 2020-12-06 06:47
    $('link[rel="stylesheet"]').attr('disabled', 'disabled');
    

    this should disable all of them, then the opposite to renable them:

    $('link[rel="stylesheet"]').removeAttr('disabled');
    
    0 讨论(0)
  • 2020-12-06 06:47

    To disable all stylesheets:

    $('link[rel~="stylesheet"]').prop('disabled', true);
    

    To re-enable all stylesheets:

    $('link[rel~="stylesheet"]').prop('disabled', false);
    

    I use the ~= attribute selector here instead of = so that the selector will still work with stylesheets where the rel attribute is alternate stylesheet, not just stylesheet.

    Also, it is important to use .prop, not .attr. If you use .attr, the code will not work in Firefox. This is because, according to MDN, disabled is a property of the HTMLLinkElement DOM object, but not an attribute of the link HTML element. Using disabled as an HTML attribute is nonstandard.

    0 讨论(0)
  • 2020-12-06 06:47
    $('link[rel=stylesheet][href~="somelink.com"]').attr('disabled', 'true');
    
    0 讨论(0)
  • 2020-12-06 06:49

    Here is a rough example that relies on assigning an id to a style and then removing and restoring it using a variable.

    HTML

    <style id="test">
    .test{
      background: red;
      width: 100px;
      height: 100px;
    }
    </style>
    <div class="test">Something</div>
    <div id="remove">Click to Remove</div>
    <div id="restore">Click to Restore</div>
    

    Javascript

    var css = $("#test");
    $("#remove").click(function(){
        css.remove();
    });
    
    $("#restore").click(function(){
        $("head").append(css);
    });
    

    Working Example http://jsfiddle.net/Fwhak/

    0 讨论(0)
提交回复
热议问题