如何在现有的svg-object上创建JQuery包装器? [英] How to create JQuery wrapper on existing svg-object?
本文介绍了如何在现有的svg-object上创建JQuery包装器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在类似的主题中,我问过这是否可行(http://stackoverflow.com/questions/6006364/svg-through-the-jquery-svg),但是当我尝试实现它时:
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="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' });
}
我遇到错误:
'TypeError:无法获取属性'createElementNS'的值:object为null或undefined'error ...
'TypeError: Unable to get value of the property 'createElementNS': object is null or undefined' error...
为什么?如何解决?
谢谢!
推荐答案
你想要获取第一个DOM元素,而不是数组。使用数组访问器或 .get(0)
:
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' });
这里的工作示例: http://jsfiddle.net/nrabinowitz/kJCAr/
这篇关于如何在现有的svg-object上创建JQuery包装器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文