最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to disable a STYLE element in HTMLJS? - Stack Overflow

programmeradmin1浏览0评论

We have a style node which we would like to write within the HTML document but, which we wouldn't like to be handled... just to keep for further use, The reason we do not use just a hidden node is because we want to preserve syntax highlighting while Dev.

In Javascript this can be done by changing the type attribute

for example:

<script type="gzip/text"></script>

would cause the script not to be executed...

is it possible to do the same on style nodes?

We have a style node which we would like to write within the HTML document but, which we wouldn't like to be handled... just to keep for further use, The reason we do not use just a hidden node is because we want to preserve syntax highlighting while Dev.

In Javascript this can be done by changing the type attribute

for example:

<script type="gzip/text"></script>

would cause the script not to be executed...

is it possible to do the same on style nodes?

Share Improve this question edited Jan 30, 2019 at 13:29 colxi 8,6703 gold badges47 silver badges44 bronze badges asked Jan 30, 2019 at 12:32 user4602228user4602228 2
  • use this code <style> /*css code here*/ </style> – Mohit Gupta Commented Jan 30, 2019 at 12:37
  • Yeah or rename <style> to something else like <stylez> and it will cause the CSS inside to be inactive – Jeremy Thille Commented Jan 30, 2019 at 12:51
Add a comment  | 

5 Answers 5

Reset to default 19

You can enable/disable a style element, using the media attribute.

By setting it to a value that will not match any device, you are actually disabling it.

<style media="max-width: 1px">
   /* Nothing contained in this styles element will be applied */
</style>

By dynamically setting and removing this attribute and its value, you can emulate the enabled/disabled behavior, as in the following example:

document.getElementById('but').addEventListener('click', function toggle(){
  let styleEl = document.getElementById('styles')
  // toggle enabled/disabled
  if(styleEl.hasAttribute('media')) styleEl.removeAttribute('media')
  else styleEl.setAttribute('media', "max-width: 1px")
})
<style id="styles" media="max-width: 1px">
   div { color: red }
</style>

<div id="but">Click me</div>

You could also disable it by changing type to text.

se here is an example

function toggle(item){
   if (typeof item  === "string")// its an id 
     item = document.getElementById(item);
   if (item.getAttribute("type") == "text")
       item.removeAttribute("type");
   else item.setAttribute("type", "text"); // this will disable it 
}
<style id="styleOne">
.active{
color:red;
}
</style>


<p onclick="toggle('styleOne')" class="active">Toggle Style</p>

You can set the disabled property in JavaScript. If set to true, the CSS rules in the element will not be applied.

function toggleEnabled(stylesheet){
    stylesheet.disabled = !stylesheet.disabled;
}

Demo:

function toggleEnabled(stylesheet){
    stylesheet.disabled = !stylesheet.disabled;
}
document.querySelector('button').addEventListener('click', function(e){
  toggleEnabled(document.getElementById('myStyles'));
});
<style id="myStyles">
body {
  background-color: dodgerblue;
}
</style>
<button>Toggle Stylesheet</button>

Some improvements to @Alen.Toma's answer

function toggleStyleById(id, turn = ""){
    item = document.querySelector("style#" + id);

    if (turn == "on") {
        item.removeAttribute("type");
        
    } else if (turn == "off") {
        item.setAttribute("type", "text");
        
    } else if (turn == "") { //Toggle
        if (item.getAttribute("type") == "text") //It's off
            item.removeAttribute("type");
            
        else //doesn't have type attrib, or its type is "text/css"
            item.setAttribute("type", "text");
    }
}

You can enable/disable a style element, using the media attribute. By setting it to a value that will not match any device, you are actually disabling it. You could also disable it by changing the type to text. You can set the disabled property in JavaScript

发布评论

评论列表(0)

  1. 暂无评论