|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi all,
I'm altering a page whose background color being set in CSS somewhere. But I need to change the background color in Javascript. I tried doing so with this: document.bgcolor = "red"; alert(document.bgcolor); When run, the value is apparently set because the alert displays "red", however the page itself does not change color. Anybody know what's going on? Thanks. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On 19 Mar 2008, wrote:
> Hi all, > > I'm altering a page whose background color being set in CSS > somewhere. But I need to change the background color > in Javascript. I tried doing so with this: > > document.bgcolor = "red"; > alert(document.bgcolor); > > When run, the value is apparently set because the alert displays > "red", however the page itself does not change color. > > Anybody know what's going on? Yes, but it isn't you... "document.bgcolor" is SO wrong it flabbergasts me! Google for "document.getElementById" and the css setting "background" for further information. -- Neredbojias http://www.neredbojias.com/ Great sights and sounds |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
> Yes, but it isn't you... "document.bgcolor" is SO wrong it flabbergasts > me! Google for "document.getElementById" and the css setting "background" > for further information. The body tag has no id, nor can I set one. However I did this var foo = getelementbytagname("body") foo.style.background = "red" ....and nothing changed. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Oops, I meant I did this
var foo = document.getelementbytagname("body") foo.style.background = "red" ...and nothing changed. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
This worked var foo=document.getElementsByTagName("body") foo[0].style.background="red" |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Scripsit test9991014@yahoo.com:
> The body tag has no id, nor can I set one. So whose document are you playing with, and why? > var foo = getelementbytagname("body") > foo.style.background = "red" When everything else fails, read the ******* manual. In this case, start from a primer on JavaScript, preferably a mordern one which is based on W3C DOMs. This is simple sample code, just to prove that you need to actually study JavaScript before trying to use it: var body = document.getElementsByTagName("body")[0]; body.style.backgroundColor = "red"; Note that JavaScript is case sensitive (and typically uses camelCase for identifiers). Beware that this code needs to be placed so that when it is executed, the document, including the body element, has been loaded and parsed. Followups trimmed. This is about JavaScript and DOM, not CSS, so both group choices were wrong, but alt.html (a catchall group) is less wrong. -- Jukka K. Korpela ("Yucca") http://www.cs.tut.fi/~jkorpela/ |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
test9991014@yahoo.com wrote:
> This worked > > var foo=document.getElementsByTagName("body") > foo[0].style.background="red" Yes it s when you a) use the correct function name, yes it is case sensitive b) take note of the return value It "works" if client has JavaScript enabled. -- Take care, Jonathan ------------------- LITTLE WORKS STUDIO http://www.LittleWorksStudio.com |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
test9991014@yahoo.com wrote:
> Hi all, > > I'm altering a page whose background color being set in CSS > somewhere. But I need to change the background color in Javascript. I > tried doing so with this: > > document.bgcolor = "red"; This doesn't alter any style. It doesn't do anything, in fact, other than add a previously nonexistent field called "bgcolor" to the document object and set its value to "red". Now, the BODY tag in transitional HTML has an attribute called "bgcolor", but in Javascript, for whatever reason, it's called document.bgColor, and names are case-sensitive in Javascript. Still, this has nothing to do with any properties that have been set with CSS. A CSS property for an element is set in Javascript as follows: var element = ...; element.style.propertyName = "..."; The name of the CSS background color property in Javascript is "backgroundColor". > alert(document.bgcolor); > > When run, the value is apparently set because the alert displays > "red", however the page itself does not change color. |
|
![]() |
| Outils de la discussion | |
|
|