How to create JQuery wrapper on existing svg-object?
In similar topic I've asked if that is possible at all (http://stackoverflow.com/questions/6006364/svg-through-the-jquery-svg), but when I tried to implement that:
<body onload="alert('loaded');testSvg();">
<div id="div3" style="border: solid 1px black; width:200px; height:100px">
<svg id="svg3" xmlns="开发者_JS百科http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"></svg>
</div>
<script type="text/javascript">
function testSvg() {
var svgRootElement = $("#svg3").get();
var svgWrapper = new $.svg._wrapperClass(svgRootElement);
svgWrapper.circle(30, 25, 50, { fill: 'red' });
}
I've got an error:
'TypeError: Unable to get value of the property 'createElementNS': object is null or undefined' error...
Why? How to fix that?
Thanks!
You want to get the first DOM element, not the array. Use an array accessor or .get(0)
:
var svgRootElement = $("#svg3")[0];
console.log(svgRootElement);
var svgWrapper = new $.svg._wrapperClass(svgRootElement);
svgWrapper.circle(30, 25, 50, { fill: 'red' });
Working example here: http://jsfiddle.net/nrabinowitz/kJCAr/
精彩评论