Changing CSS Values with Javascript

后端 未结 9 1412
借酒劲吻你
借酒劲吻你 2020-11-22 17:16

It\'s easy to set inline CSS values with javascript. If I want to change the width and I have html like this:

<
相关标签:
9条回答
  • This simple 32 lines gist lets you identify a given stylesheet and change its styles very easily:

    var styleSheet = StyleChanger("my_custom_identifier");
    styleSheet.change("darkolivegreen", "blue");
    
    0 讨论(0)
  • 2020-11-22 18:01

    Ok, it sounds like you want to change the global CSS so which will effictively change all elements of a peticular style at once. I've recently learned how to do this myself from a Shawn Olson tutorial. You can directly reference his code here.

    Here is the summary:

    You can retrieve the stylesheets via document.styleSheets. This will actually return an array of all the stylesheets in your page, but you can tell which one you are on via the document.styleSheets[styleIndex].href property. Once you have found the stylesheet you want to edit, you need to get the array of rules. This is called "rules" in IE and "cssRules" in most other browsers. The way to tell what CSSRule you are on is by the selectorText property. The working code looks something like this:

    var cssRuleCode = document.all ? 'rules' : 'cssRules'; //account for IE and FF
    var rule = document.styleSheets[styleIndex][cssRuleCode][ruleIndex];
    var selector = rule.selectorText;  //maybe '#tId'
    var value = rule.value;            //both selectorText and value are settable.
    

    Let me know how this works for ya, and please comment if you see any errors.

    0 讨论(0)
  • 2020-11-22 18:02

    I don't have rep enough to comment so I'll format an answer, yet it is only a demonstration of the issue in question.

    It seems, when element styles are defined in stylesheets they are not visible to getElementById("someElement").style

    This code illustrates the issue... Code from below on jsFiddle.

    In Test 2, on the first call, the items left value is undefined, and so, what should be a simple toggle gets messed up. For my use I will define my important style values inline, but it does seem to partially defeat the purpose of the stylesheet.

    Here's the page code...

    <html>
      <head>
        <style type="text/css">
          #test2a{
            position: absolute;
            left: 0px;
            width: 50px;
            height: 50px;
            background-color: green;
            border: 4px solid black;
          }
          #test2b{
            position: absolute;
            left: 55px;
            width: 50px;
            height: 50px;
            background-color: yellow;
            margin: 4px;
          }
        </style>
      </head>
      <body>
    
      <!-- test1 -->
        Swap left positions function with styles defined inline.
        <a href="javascript:test1();">Test 1</a><br>
        <div class="container">
          <div id="test1a" style="position: absolute;left: 0px;width: 50px; height: 50px;background-color: green;border: 4px solid black;"></div>
          <div id="test1b" style="position: absolute;left: 55px;width: 50px; height: 50px;background-color: yellow;margin: 4px;"></div>
        </div>
        <script type="text/javascript">
         function test1(){
           var a = document.getElementById("test1a");
           var b = document.getElementById("test1b");
           alert(a.style.left + " - " + b.style.left);
           a.style.left = (a.style.left == "0px")? "55px" : "0px";
           b.style.left = (b.style.left == "0px")? "55px" : "0px";
         }
        </script>
      <!-- end test 1 -->
    
      <!-- test2 -->
        <div id="moveDownThePage" style="position: relative;top: 70px;">
        Identical function with styles defined in stylesheet.
        <a href="javascript:test2();">Test 2</a><br>
        <div class="container">
          <div id="test2a"></div>
          <div id="test2b"></div>
        </div>
        </div>
        <script type="text/javascript">
         function test2(){
           var a = document.getElementById("test2a");
           var b = document.getElementById("test2b");
           alert(a.style.left + " - " + b.style.left);
           a.style.left = (a.style.left == "0px")? "55px" : "0px";
           b.style.left = (b.style.left == "0px")? "55px" : "0px";
         }
        </script>
      <!-- end test 2 -->
    
      </body>
    </html>
    

    I hope this helps to illuminate the issue.

    Skip

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