How to change the color of a div before loading the page through a script?

1

I'm trying to choose the background color of a div through a script. That is, my goal is that as soon as the page appears it has already run the script and the color that the script selected has appeared.

My question for now is:

<body onload="funcaoquemudaacor()">

Apparently, the onload parameter does not seem to me to be the right one, and is not working.

Is that my mistake? Solutions?

    
asked by anonymous 17.04.2015 / 01:19

4 answers

3

If the goal is to change the color of a particular div, the first time this can be done is how the div exists on the page. For example:

<!-- esta é a sua div: -->
<div id="minhadiv">...</div>
<script>
funcaoquemudaacor();
</script>

Of course, funcaoquemudaacor also already needs to exist at that time, so it should be declared in the same block <script> as the call, or included before that block if it is an external js file.

The% w_that you used expects the entire page to load, including all images and other external files.

    
17.04.2015 / 05:39
1

Try using the style attribute:

<div style="background-color: #000000">

</div>
    
17.04.2015 / 21:57
0

How to declare in onload is correct .... please give us the function please ... you can test it will probably work

 <script>
       function funcaoQueMudaDeCor(){
         document.getElemmentById('idDaDiv').style.backgroundColor= "#000"; 
      }
 </script>

Remember that for this function to work what comes after the style. must be the same property assigned in div's css. If you try with a div declared in the css with the attribute "background:" it will probably not be overridden by the js "backgroundColor" code.  QQer thing put all js and css q help.

    
17.04.2015 / 18:55
-2

Use jquery .ready. I think it works. I think you'll have to put the script in <head>

    
17.04.2015 / 01:23