(使用http://code.google.com/p/svgweb/

window.onsvgload = function() {
  function onEnter(e) {
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
      targ = targ.parentNode;

    alert(targ.hasAttributeNS(null, 'id')); //displays false
    alert(targ.getAttributeNS(null, 'id')); //displays a blank dialog
    alert(targ.id); //displays a blank dialog
  }

  /* Seldom_Seen is a group element in the SVG - <g id="Seldom_Seen">... */
  document.getElementById('Seldom_Seen').addEventListener("mouseover", onEnter, false); //alert is blank
  document.getElementById('normal_dom_element').addEventListener("mouseover", onEnter, false); //alert displays id as expected
}


事件侦听适用于SVG元素。我似乎无法获取ID。我可以获取其他对象属性,例如x,y值。无论如何要获取目标元素的ID?

最佳答案

也许

e.currentTarget


但是使用jQuery,它只是

window.onsvgload = function() {
  $('#Seldom_Seen').mouseover(function(){ onEnter(this); });
  $('#normal_dom_element').mouseover(function(){ onEnter(this); });
}

function onEnter(obj) {
  alert($(obj).attr("id"));
}

08-19 16:34