javascript - Setting document.body.className as a variable -


When a user clicks on a link, this is my code to switch to my body tags class.

  function switchBodycore () {if (document.body.className == 'blue') document.body.className = 'red'; Else if (document.body.className == 'red') document.body.className = 'green'; Else if (document.body.className == 'green') document.body.className = 'blue'; }  

I want to set the resulting color as a variable named bodyColor. So if the body class is "blue" and the user clicks on and switches it to "red" then I want red to be red later in the form of variables for other uses. Or better yet, set the document. Body.className as a self, and then switch the document. In that computer with the BowdierColver () function. Call.ClassName.

I thought on the lines of this:

  if (document.body.className == 'blue') document.body.className = 'red', var bodyColor = Red; To set the body class as a variable,  

or

color = color =

Of course, none of those two options work either ^ _ ^; How can I complete either the above (or both)?

You need your variable to be global:

  Var bodyColor = 'red'; // GlobalWare, which has been started in your first color class function switchBodialer () (if (document.body.className == 'blue') document.body.className = 'red'; Else if (document.body. ClassName == 'red') document.body.className = 'green'; Else if (document.body.className == 'green') document.body.className = 'blue'; BodyColor = document.body.className; Warning (BodyColor);}  

In your second example, you also need to quote around your color string:

  bodyColor = "red" ;  



Another way to do this may be the number of your color squares , Which will allow you to use simple arithmetic to change your orbits and allow you to easily change the number of sections you are cycling through.

  color color = 0; Var Total Colors = 3; Function SwitchBodieRolar () {Column = (Color + 1)% totalcolors; Document.body.className = 'Color' + Colorname;}  

You CSS will be:

  .color0 {background color: blue; } .color1 {background color: red; } .color2 {background color: green; }  

Or whatever your color class definitions are.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -