jQuery 附加到 AJAX 加载的 SVG 问题
我成功地通过 AJAX 从外部文件加载了一些 svg:
$("#svg").load(svgUrl + " svg", function() {
// do stuff
});
我的 HTML 看起来像这样:
<div id="svg" ...>
<svg ...>
...
</svg>
</div>
我可以很好地看到图形。现在我想向加载的 svg 添加一些额外的 svg 元素。我将脚本更改为:
$("#svg").load(svgUrl + " svg", function() {
$("svg").append("<g id='compass'></g>");
// do stuff
});
由于某些原因,添加的元素显示为隐藏在 firebug 中,无论我在其中放入什么 xml,我都无法在网页上看到它。
更新:
感谢 echo-flow 我能够附加到我的 SVG。现在,如果我尝试从另一个 xml 文件加载指南针 svg,它不会出现在我的 DOM 中。目前我的代码如下所示:
$("#svg").load(obj.svgUrl + " svg", function() {
var svgns = "http://www.w3.org/2000/svg";
var g = document.createElementNS(svgns,"g");
g.setAttributeNS(null,'id','compass');
$("svg").append(g);
$("#compass").load("files/svg/compass.xml");
});
如果我在 firebug 的控制台中查看,我会看到罗盘标记的 AJAX 请求的结果成功但为空。
I am successfully loading via AJAX some svg from external file:
$("#svg").load(svgUrl + " svg", function() {
// do stuff
});
My HTML looks like that:
<div id="svg" ...>
<svg ...>
...
</svg>
</div>
I can see the graphics just fine. Now I want to add some additional svg elements to the loaded svg. I changed my script to:
$("#svg").load(svgUrl + " svg", function() {
$("svg").append("<g id='compass'></g>");
// do stuff
});
For some reasons the added element appears as hidden in firebug and no matter what xml I put inside of it I can't see it on my webpage.
Update:
Thanks to echo-flow I was able to append to my SVG. Now if I try to load my compass svg from another xml file it doesn't appear in my DOM. At the moment my code looks like:
$("#svg").load(obj.svgUrl + " svg", function() {
var svgns = "http://www.w3.org/2000/svg";
var g = document.createElementNS(svgns,"g");
g.setAttributeNS(null,'id','compass');
$("svg").append(g);
$("#compass").load("files/svg/compass.xml");
});
If I look in console in firebug I see that result of the AJAX request for compass markup is successful but is empty.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
jQuery 并不是真正为识别 XML 命名空间而构建的,因此字符串
" "
可能会被解析,以便生成的 DOM 节点在默认命名空间中,而不是 SVG 命名空间。您可以通过使用常规 DOM 创建节点来解决此问题。这看起来如下所示:如果您需要创建更复杂的结构,那么我建议查看 jquery-svg 库,它具有用于生成 SVG DOM 的更清晰的 API。
已更新
我误解您正在尝试加载 SVG 文档并将其附加到您的主机 HTML 文档 - 相反,我认为您正在尝试使用脚本生成 SVG。为了解决您的问题,我建议执行以下操作(未经测试,但应该有效):
jQuery is not really built to be aware of XML namespaces, so the string
"<g id='compass'></g>"
is likely getting parsed such that the generated DOM nodes are in the default namespace, as opposed to the SVG namespace. You can solve this by using regular DOM to create the nodes. This would look like the following:If you need to create more complex structures, then I would recommend looking at the jquery-svg library, which has a cleaner API for generating SVG DOM.
Updated
I misunderstood that you were trying to load an SVG document and append it to your host HTML document - instead, I thought you were trying to generate SVG using script. To solve your problem, I would recommend doing the following (not tested, but should work):
如果您开始使用jquery canvas,可能会更容易。它是一种模块化语言,用于在 XML 中描述二维矢量和混合矢量/光栅图形
It might be easier if you start using jquery canvas. which is a modularized language for describing two-dimensional vector and mixed vector/raster graphics in XML
我让它像这样工作。
I got it to work like this.