yawnmoth said:
I'm trying to write a script whereby the body background is changed to
back (from white) when the mouse button is held down and kept held
down. When you lift it up, I'd like it to change back to white.
Unfortunately, I'm not sure how to do that. Any ideas? Here's my
code:
http://www.frostjedi.com/terra/scripts/demo/onmousedown.html
Now, that is so little code that you could have posted it, too:
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
| "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
| <html xmlns="
http://www.w3.org/1999/xhtml" xml:lang="en" >
You do not want to use XHTML. Use HTML 4.01 instead.
| <head>
| <title>onmousedown / onmouseup</title>
| </head>
|
| <body id="body">
The `body' element does not need an ID to be referred to.
| <script type="text/javascript">
| body = document.getElementById('body');
`body' was not declared, which is error-prone. And you don't need the
additional identifier anyway:
document.body
is mostly standards-compliant:
<
http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-62018039>
| document.onmouseup = body.style.background = '#fff';
| document.onmousedown = body.style.background = '#000';
It if works, this is assigning "#fff" to `document.onmouseup' and "#000" to
`document.onmousedown' because the right-hand side of the whole assignment
evaluates to the result of the right-hand side assignment.
Because there is no delay in the evaluation of the right-hand side
assignment, the background color of the `body' element would turn white and
immediately black afterwards.
You should not use the `background' CSS property when all you want to do is
to change the background color.
The `onmouseup' and `onmousedown' properties are proprietary and should not
be used but as a fallback for the standards-compliant approach.
It is not necessary here to use scripting to define the event listeners; the
HTML BODY element (and the XHTML `body' element) has intrinsic event handler
attributes for that.
<body
onmouseup="document.body.style.backgroundColor = '#fff';"
onmousedown="document.body.style.background = '#000';">
(Usually, by convention you could refer to the handling element with `this',
but the `body' element has a peculiarity here.)
RTFM.
<
http://jibbering.com/faq/>
PointedEars