HTML “link” (stylesheet) disabled attribute

前端 未结 3 1781
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-01 22:44

I\'m using JavaScript to enable/disable stylesheets using the following:

document.styleSheets[0].disabled = true|false;

This JS works fine,

相关标签:
3条回答
  • 2021-01-01 23:20

    I think in this case you may have to rely on JavaScript.

    If you think more down the lines of serving up the style-sheet if needed rather than disabling by default you should nail it.

    0 讨论(0)
  • 2021-01-01 23:29

    Why not turning the problem around : only load the CSS if JavaScript is enabled?

    Taken from this example, you could use something like:

    var fileref=document.createElement("link")
    fileref.setAttribute("rel", "stylesheet")
    fileref.setAttribute("type", "text/css")
    fileref.setAttribute("href", filename)
    
    0 讨论(0)
  • 2021-01-01 23:38

    You can use JavaScript's removeAttribute method for that.

    <html>
        <head>
            <link id="first_style" rel="stylesheet" href="#" disabled />
    
            <script type="text/javascript">
                window.onload = function()
                {
                    document.getElementById('first_style').removeAttribute('disabled');
                }
            </script>
        </head>
        <body>
            <p>something</p>
        </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题