Define css if javascript is not enabled

前端 未结 8 1635
无人共我
无人共我 2020-12-10 14:58

I want to define .exampleclass img {height:250px} if javascript is not enabled. Is their anyway to undo this in javascript / jquery?

相关标签:
8条回答
  • 2020-12-10 15:03

    put a "no-js" class on your body element, and then remove it with JS at load, and use .no-js in the selector for your CSS

    0 讨论(0)
  • 2020-12-10 15:05

    Best not to mess with CSS via JS in that way. Try this:

    <noscript>
    
        <link href="no-js.css" type="text/css" rel="stylesheet" />
    
    </noscript>
    
    0 讨论(0)
  • 2020-12-10 15:06

    I use something like this...

    JavaScript

    document.body.className += ' javascript';
    

    jQuery

    $('body').addClass('javascript');
    

    CSS

    .exampleclass img {height: 250px}
    
    .javascript .exampleclass img {height: 500px}
    
    0 讨论(0)
  • 2020-12-10 15:06

    The most elegant way is to have in your CSS something like:

    .exampleclass img {height:250px}
    .js .exampleclass img {height:auto;}
    

    and in your JS:

    var dd = document.documentElement;
    dd.className += ((dd.className == "") ? "js" : " js");
    

    so that the second CSS rule will override the first one in case JS is enabled

    0 讨论(0)
  • 2020-12-10 15:18

    In the future (if and when browsers start supporting it), it will be possible to use the scripting CSS media feature:

    @media (scripting: none) {
      // Styles to apply if JS is disabled
    }
    
    0 讨论(0)
  • 2020-12-10 15:22

    Try

    <noscript>
        <style type="text/css">
            .exampleclass img {height:250px}
        </style>
    </noscript>
    

    Noscript

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