(使用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"));
}
关于javascript - 动态获取 svg 元素 'id' 属性(Google SVG web),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3169497/