Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>SVG element test</title>
<script type="text/javascript">
window.onload = function()
{
alert(document.getElementById("svg_elem").offsetParent);
}
</script>
</head>
<body>
SVG element test:
<div id="svg_wrapper">
<svg id="svg_elem" xmlns="http://www.w3.org/2000/svg" version="1.1" style="width:600px; height:400px;"/>
</div>
</body>
</html>
On loading, offsetParent for svg_elem is reported as undefined. I guess it's because svg is using a different namespace but the original problem really is, how to get a relative offset in an svg element, based on mouse event. I thought to do it using the absolute cursor position which comes from mouse event and offsetParent, offsetTop etc. (as described here: http://www.quirksmode.org/js/findpos.html ) but they don't work at all for an <svg> which is defined within xhtml. So is there another way to do it?
Thanks.