Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #14633

Re: Can't get onmouseover to work

Newsgroups comp.lang.javascript
Subject Re: Can't get onmouseover to work
From "Evertjan." <exxjxw.hannivoort@inter.nl.net>
References <b6350f1a-96f5-4508-9ac7-c7b9c597600d@googlegroups.com>
Message-ID <XnsA07BF2F36DC04eejj99@194.109.133.133> (permalink)
Date 2012-06-23 21:52 +0000

Show all headers | View raw


 wrote on 23 jun 2012 in comp.lang.javascript:

> All,
> 
> I'm on page 115 of Resig's Pro Javascript Techniques (2006) and I
> can't get the following code to work. When the mouse hovers over the
> link, nothing happens to the background color. 
> 
> Any ideas?
> 
> David.
> 
> <!DOCTYPE html>
> <html>
> <head>
> <title>Pro Javascript -- Events</title>
> 
> <script>
> window.onload = loaded;
> 
> function loaded() {
>      document.getElementById('body').style.border="1px solid #000";
>}
> 
> var li = document.getElementsByTagName("li");
> for (var i = 0; i < li.length; i++) {
>      li[i].onmouseover = function() {
>           this.style.backgroundColor = "blue";
>     };
>      li[i].onmouseout = function() {
>           this.style.backgroundColor = "white";
>     };
>}
> </script>

You made some errors in "}" positioning,
not visible due to incorrect indenting

Try this:

<script type='text/javascript'>
window.onload = loaded;

function loaded() {
  document.getElementById('body').style.border="1px solid #000";

  var li = document.getElementsByTagName("li");
  for (var i = 0; i < li.length; i++) {
    li[i].onmouseover = function() {
      this.style.backgroundColor = "blue";
    };
    li[i].onmouseout = function() {
      this.style.backgroundColor = "white";
    };
  };
};
</script>




> </head>
> 
> <body id="body">
> <h1>Pro Javascript -- Events</h1>
> 
> <p>Now is the time.</p>
> 
> <ul>
>      <li><a href="#">First Link</a></li>
>      <li><a href="#">Second Link</a></li>
> </ul>
> 
> </body>
> </html>
> 



-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Can't get onmouseover to work dwarnold45@suddenlink.net - 2012-06-23 14:07 -0700
  Re: Can't get onmouseover to work Bjoern Hoehrmann <bjoern@hoehrmann.de> - 2012-06-23 23:21 +0200
    Re: Can't get onmouseover to work Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-06-24 04:00 +0200
  Re: Can't get onmouseover to work "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-06-23 21:52 +0000
  Re: Can't get onmouseover to work dwarnold45@suddenlink.net - 2012-06-23 17:35 -0700

csiph-web