Change Background Color

Discussion in 'Javascript' started by shapper, Oct 19, 2006.

  1. shapper

    shapper Guest

    Hello,

    How can I change the background color of a DIV when the mouse is over
    it?

    Thank You,
    Miguel
     
    shapper, Oct 19, 2006
    #1
    1. Advertising

  2. shapper

    Benjamin Guest

    here's the script:
    <script type="text/javascript">
    function changeTo(div) {
    div.style.backgroundColor = "red";
    }
    function changeFrom(div) {
    div.style.backgroundColor = "white";
    }
    </script>

    Your div should like somthing like this:
    <div onmouseover="changeTo(this)" onmouseout="changeFrom(this)">Some
    stuff</div>

    shapper wrote:
    > Hello,
    >
    > How can I change the background color of a DIV when the mouse is over
    > it?
    >
    > Thank You,
    > Miguel
     
    Benjamin, Oct 19, 2006
    #2
    1. Advertising

  3. shapper

    ASM Guest

    Benjamin a écrit :
    > here's the script:


    and here's another one :

    css :
    =====
    ..onRed { background: lightyellow; }
    ..onRed:hover { background: yellow; }

    javascript :
    ============
    var IE = false; /*@cc_on IE=true; @*/

    function roll(what) {
    if(IE) {
    what = what.style;
    what.backgroundColor = what.backgroundColor==''? 'yellow' : '';
    }
    }

    html :
    ======
    <div class="onRed"
    onmouseover="roll(this);"
    onmouseout="roll(this);">
    bla bla blabla
    </div>
     
    ASM, Oct 19, 2006
    #3
  4. shapper

    RobG Guest

    ASM wrote:
    [...]
    > css :
    > =====
    > .onRed { background: lightyellow; }
    > .onRed:hover { background: yellow; }
    >
    > javascript :
    > ============
    > var IE = false; /*@cc_on IE=true; @*/


    IE 7 is supposed to support hover on elements other than A, so you
    might want to reconsider using browser sniffing with script and switch
    to pure CSS.


    --
    Rob
     
    RobG, Oct 19, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ather Ali Shaikh

    How do i change background color to transparent

    Ather Ali Shaikh, Dec 4, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    4,341
    Chris Jackson
    Dec 4, 2003
  2. Craig Duffy

    Can't change textbox background color

    Craig Duffy, Aug 17, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    1,070
    Seanray
    Sep 10, 2004
  3. fig000
    Replies:
    0
    Views:
    4,566
    fig000
    Sep 6, 2004
  4. Kamaljeet Saini
    Replies:
    0
    Views:
    446
    Kamaljeet Saini
    Feb 13, 2009
  5. Replies:
    1
    Views:
    279
    marss
    Feb 14, 2007
Loading...

Share This Page