【发布时间】:2013-04-01 07:40:57
【问题描述】:
我正在尝试使用 jquery/javascript 获取 svg 文件 svg 示例:
<svg width="111" height="123" xmlns="http://www.w3.org/2000/svg">
<g>
<title>Layer 1</title>
<rect fill="#ffffff" stroke="#000000" stroke-width="3" x="1.5" y="1.5"width="108.00001" height="119.99999" id="svg_1" rescale="none" move="Static"/>
<text text_pos="midcenter" xml:space="preserve" text-anchor="middle" font-family="Fantasy" font-size="14" id="Actor Role name" y="68" x="55" stroke-width="0" stroke="#000000" fill="#000000" rescale="none" move="Static">Actor Role</text>
</g>
</svg>
并使用类似的方法从文件中提取数据
$.get(file_url, function(data) {
var teste=data;
},'xml')// or use text instead of xml
然后获取所有元素,如 rect 或 text 并说得到类似的东西(排除内部''只是为了知道值来自哪里):
'元素'矩形,'重新缩放'无,'移动'静态
对于文本(不包括里面的''): 'Element' rect, 'rescale' none, 'move' static, 'text_pos' midcenter ,'id' Actor Role name, 'node value' Actor Role
部分解决
$.get(file_url, function(data) {
var teste=data; //all data
rect1=$('<g>').append($(teste).find("text").attr("id")).html();
rect2=rect1+"-"+$('<g>').append($(teste).find("text").attr("text_pos")).html();
alert(rect2);
});
alert(rect2);
问题发现它没有通过 $.get 之外的变量数据
首先alert(rect2); 给出正确的数据
第二个alert(rect2); 给我未定义
任何人都知道它为什么不提供全局变量:X 已经尝试在外面制作变量但也不起作用
很抱歉忘记更改评论:f 现在它是正确的
【问题讨论】:
-
你的代码中没有
alert(rect1),只有2个alert(rect2)。 2nd 未定义是完全正常的,因为:1- 它在上述函数中定义 2- 它在 ajax 回调中定义(即...异步) -
我更改了代码区域但忘记更改其余部分,现在它是正确的
标签: javascript jquery xml svg