繁体   English   中英

动态获取svg元素“ id”属性(Google SVG网站)

[英]Obtaining svg element 'id' attribute dynamically (Google SVG web)

(使用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"));
}

您很可能会得到另一个预期之外的目标。 您在'Seldom_Seen'元素内的所有元素都具有ID吗? 您始终可以提醒目标以查看其是什么类型的元素。

<g>元素上也有一些鼠标悬停/鼠标悬停的陷阱,事件冒泡,除非进行一些过滤,否则您可能会两次获得它们。 有关可能的解决方法,请参见此处的幻灯片17和18

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM