views:

200

answers:

1

Hi,

When using :

document.onmouseover = function(e) {}

Is there a property which gives me the element in the dom tree ?

For example, I can set a style to e.srcElement But, how can I later access this element to (for example) reset its style ? And how can I know at which place in the dom tree it is ? I want to be able to situate it in the whole page dump.

Many thanks.

To solve the problem about reaccessing the element later, I tried this but it doesn't work :

var lastelem;

document.onmouseover = function(e) {

  if (lastelem != null){
    lastelem.style.border = "0px";
  }

  if (e===undefined) e= window.event;
  var target= 'target' in event? event.target : event.srcElement;
  document.getElementById('display').value = target.tagName;
  target.style.border = "1px";
  lastelem = target;
};

Thanks

+2  A: 

Which HTML element is the target of the event? (on Quirksmode.org, by Peter-Paul Koch). Also have a look at the target attribute of the Event object in W3C DOM Level 2 Event Model.

<!DOCTYPE html>
<html>
  <head>
    <title>target test</title>
    <style>
      *        { border: 1px solid #fff }
      .trigger { background: lightgreen }
    </style>
  </head>
  <body>
    <p class="trigger">Trigger testCase().</p>
    <p class="trigger">Trigger testCase().</p>
    <p class="trigger">Trigger testCase().</p>
    <p id="display"></p>
    <script>
var lastelem;

document.onmouseover = function (e) {
        var event = e || window.event;

        if (lastelem) {
                lastelem.style.border = "1px solid #fff";
        }

        var target = event.target || event.srcElement;
        document.getElementById('display').innerHTML = target.previousSibling.tagName +
            " | " + target.tagName + " | " + (target.nextSibling ? target.nextSibling.tagName : "X");
        target.style.border = "1px solid";
        lastelem = target;
};
    </script>
  </body>
</html>
Marcel Korpel
Yes, I've already read this. Thanks.What I need is a documentation with all the properties of parentElement, srcElement/target,...And also a way to locate the element in the dom tree. Say, if I want to highlight the parentElement in the source from the event I received with the mouseover. How can I achieve this ?
oimoim
Marcel Korpel : This works very well ! Thanks.Now, I just need to find a way to show the surrounding tags. I think I will try with previoussibling and nextsibling.
oimoim
@oimoim: Indeed: http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#attribute-previousSibling
Marcel Korpel
Thanks for your help ;)
oimoim