Can I use CSS to specify event handlers ?

Discussion in 'Javascript' started by Richard A. DeVenezia, Sep 1, 2003.

  1. Can I use CSS to set onmouseover ?

    In HTML I have a bunch of <A HREF="whatever"
    onmouseover=eventhandler(this)">stuff</A>. I would prefer not to have the
    onmouseover 'pollution'

    Is it possible to specify the eventhandler using css ? i.e.

    <A HREF="whatever" class="foo">stuff</A>.

    A.foo { on-mouse-over: eventhandler(this) }
     
    Richard A. DeVenezia, Sep 1, 2003
    #1
    1. Advertisements

  2. No.

    CSS is all about presentation. It is about how things look, not how
    they act.
    You can assign them with Javascript instead.

    document.links['aId'].onmouseover = eventhandler;

    Make a lot of these in a function that is run when the page is loaded,
    So, no.

    <hack>
    There is one ugly thing you can do: make URLs with the Javascript:
    pseudo protocol in CSS.

    Try adding:
    a.foo:hover { background: url("javascript:alert('Gotcha')"); }
    and see what happens when you move over the a element :)

    Works in IE and Opera, not in Mozilla.

    In practice: Don't. This is purely for fun.
    </hack>

    /L
     
    Lasse Reichstein Nielsen, Sep 1, 2003
    #2
    1. Advertisements

  3. This is limited to a.foo:hover, which acts like an onmouseover handler. In
    IE, you can also combine it with the expression keyword:

    a.foo:hover { cssProperty:expression(myfunc()) }

    Where cssProperty is a valid css property like color, font-size, width,
    height etc. and myfunc() a user defined function that returns the property's
    value.


    JW
     
    Janwillem Borleffs, Sep 1, 2003
    #3
  4. .... which isn't CSS.
     
    David Dorward, Sep 1, 2003
    #4
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.