event handling through CSS and modifying style dynamically

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kirk Is

    #1

    event handling through CSS and modifying style dynamically

    Hi there.
    I'm trying to improve a simple javascript based graphics editor for
    Atari 2600 games (long story) with better use of CSS and Javascript.
    I'm a little weak on what CSS can and can't readily do (sometimes I'm
    stuck in a Netscape 3 world, mentally) so I have a few questions:

    Say I had an editor with some CSS like this:
    <html><head>
    <style type="text/css">
    td.edt {background-color: yellow; height: 50px; width:50px}
    </style>
    </head><body>
    <table>
    <tr>
    <td class="edt" name="00"></td>
    <td class="edt" name="01"></td>
    </tr>
    <tr>
    <td class="edt" name="10"></td>
    <td class="edt" name="11"></td>
    </tr>
    </table>
    </body></html>

    1. Is there anyway to associate event handlers like onMouseDown with
    every td of class "edt", or will I need to put the onMouseDown in the
    tag of each td ? (Or do I need to get very complex with javascript
    prototypes etc, so it would be easier just to do the individual
    onMouseDowns ?)

    2. Via javascript, can I modify the edt class so that, say, every
    element using that class will change, like color or size?

    3. Is there a better way of doing a basic graphics editor than to just
    monitor onMouseDown / onMouseUp / onMouseOver ? It seems like I might
    have a problem turning the "pen off" if the mouse leaves the table
    entirely...can I capture a onMOuseOut for the whole table, or a <div>
    surrounding it? When I tried that, it seemed like it was also setting
    up a onMouseOut for the children <td> elements...

    THanks

  • Baconbutty

    #2
    Re: event handling through CSS and modifying style dynamically

    >>1. Is there anyway to associate event handlers like onMouseDown with[color=blue][color=green]
    >>every td of class "edt", or will I need to put the onMouseDown in the[/color][/color]
    [color=blue][color=green]
    >>tag of each td ? (Or do I need to get very complex with javascript
    >>prototypes etc, so it would be easier just to do the individual
    >>onMouseDown s ?)[/color][/color]

    Some events "bubble". I.e. if you click on the TD, the event will fire
    for every parent element up to the "document".

    So you could set an event on the document thus:-

    document.onmous edown=function( eEvent)
    {
    var eEvent=eEvent|| window.event;
    var eElement=eEvent .target||eEvent .srcElement;

    if (eElement.tagNa me=="TD"){//do something;}

    };
    [color=blue]
    >2. Via javascript, can I modify the edt class so that, say, every
    >element using that class will change, like color or size?[/color]

    Yes, you will need to learn about modifying rules within styleSheet
    objects.

    See: http://www.quirksmode.org/dom/changess.html

    [color=blue]
    >3. Is there a better way of doing a basic graphics editor than to just[/color]
    [color=blue]
    >monitor onMouseDown / onMouseUp / onMouseOver ?[/color]

    Within the confines of Javascript and HTML, I don't know.
    [color=blue]
    >It seems like I might
    >have a problem turning the "pen off" if the mouse leaves the table
    >entirely...c an I capture a onMOuseOut for the whole table, or a <div>
    >surrounding it? When I tried that, it seemed like it was also setting
    >up a onMouseOut for the children <td> elements...[/color]

    This is because events "bubble". An onmouseout on a TD element, will
    also bubble up and fire the same event on the TABLE element.

    It is a problem, and I am sure others will have better solutions.

    You could try detecting whether the "srcElement " or "target" for the
    onmouseout event is the the TABLE element. But if you move the mouse
    quickly, or the table has no border, this may fail.

    Or, for all "onmouseout s" and "onmouseove r" events in the document as a
    whole, you could get the srcElement or target, and if it is not a TD
    within the table, fire your pen up event.

    IE has also "onmouseent er" and "onmouselea ve" which you might
    investigate.

    Julian

    Comment

    • Kirk Is

      #3
      Re: event handling through CSS and modifying style dynamically

      Baconbutty wrote:[color=blue][color=green]
      > >2. Via javascript, can I modify the edt class so that, say, every
      > >element using that class will change, like color or size?[/color]
      >
      > Yes, you will need to learn about modifying rules within styleSheet
      > objects.
      >
      > See: http://www.quirksmode.org/dom/changess.html[/color]

      Ugh -- might be better to iterate over the relevant elements.
      [color=blue][color=green]
      > >It seems like I might
      > >have a problem turning the "pen off" if the mouse leaves the table
      > >entirely...c an I capture a onMOuseOut for the whole table, or a[/color][/color]
      <div>[color=blue][color=green]
      > >surrounding it? When I tried that, it seemed like it was also[/color][/color]
      setting[color=blue][color=green]
      > >up a onMouseOut for the children <td> elements...[/color]
      >
      > This is because events "bubble". An onmouseout on a TD element, will
      > also bubble up and fire the same event on the TABLE element.[/color]

      A reasonably clean solution I've found is to just set the handler for
      the document:
      document.onmous eup=function(eE vent) {
      mouseIsDown = false;
      }
      I don't care *where* the mouse is released, so this works well.

      Thanks! I'm actually posting a related post w/ example code elsewhere
      here.

      Comment

      Working...