|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
|||
|
|||
|
I feel like an idiot...
I'm pretty sure I'm missing something obvious here:
Code:
//javascript:
function menuClick(menuId) {
menuId.style.backgroundImage = "url(/images/skins/greeny/menubutton-down.png)";
}
function menuUnClick(menuId) {
menuId.style.backgroundImage = "url(/images/skins/greeny/menubutton.png)";
}
//Events
document.getElementById('menu0').onmousedown=menuC lick(document.getElementById('menu0'));
document.getElementById('menu0').onmouseup=menuUnC lick(document.getElementById('menu0'));
document.getElementById('menu0').onmouseout=menuUn Click(document.getElementById('menu0'));
//menuClick etc. aren't broken up in my script, I think your board is playing up.
Code:
<!-- HTML -->
<ul id="menu">
<li id="menu0">
<a href="/index.php">Main Page</a>
</li>
</ul>
Code:
/* CSS */
#menu li {
list-style-type: none;
float: left;
background-image: url(/images/skins/greeny/menubutton.png);
}
It's meant to be a simple click-and-switch-bacground, but it Works Not. Using the HTML onLoad etc. attributes works, but then my code looks like ****. The CSS and JS are both external files. Firefox: Error: menuId has no properties Source File: http://localhost/js/menu.js Line: 7 |
|
#2
|
|||
|
|||
|
Looks like you're accessing the DOM before it's creation is complete. Do something like this:
Code:
window.onload = function() {
document.getElementById('menu0').onmousedown= menuClick(document.getElementById('menu0'));
document.getElementById('menu0').onmouseup= menuUnClick(document.getElementById('menu0'));
document.getElementById('menu0').onmouseout= menuUnClick(document.getElementById('menu0'));
}
P.S. Many forum scripts break up long lines. It certainly is annoying when posting code. |
|
#3
|
|||
|
|||
|
Well, that fixed one problem, but now the function is called when, and only when, I refresh the page.
![]() Thanks for your help though. ![]() |
|
#4
|
|||
|
|||
|
That's odd. Perhaps there's another window.onload event handler that sometimes overrides this one.
|
|
#5
|
|||
|
|||
|
hey this code will never work
because the function you are trying to call on various events , they will automatically execute as soon as the window loads. us something like this window.onload = function() { document.getElementById('menu0').onmousedown= function (){menuClick(document.getElementById('menu0'));} } |
|
#6
|
|||
|
|||
|
Wow. Thanks for catching my mistake, ravs.
![]() You could also use the this keyword. Code:
window.onload = function() {
document.getElementById('menu0').onmousedown= function (){menuClick(this);}
}
|
|
#7
|
|||
|
|||
|
It workses!
Ta muchly to all. ![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > I feel like an idiot... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|