Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquerysvg:获取外部加载的svg的属性_Jquery_Svg - Fatal编程技术网

jquerysvg:获取外部加载的svg的属性

jquerysvg:获取外部加载的svg的属性,jquery,svg,Jquery,Svg,我使用svg插件加载了一个外部svg <svg version="1.2" baseProfile="tiny" id="fig" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="200px" height="100px" viewBox="0 0 200 100" overflow="inherit" xml:space="preser

我使用svg插件加载了一个外部svg

<svg version="1.2" baseProfile="tiny" id="fig" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
x="0px" y="0px" width="200px" height="100px" viewBox="0 0 200 100" overflow="inherit" xml:space="preserve">
<ellipse id="obj" cx="100" cy="50" rx="20" ry="40"/>
</svg>
如何获取svg的属性值(id='fig')

谢谢

svg加载了用于jquery的svg插件

myDiv.css( "width", "144px" );
myDiv.css( "height", "72px" );
myDiv.svg({loadURL: "test_01.svg", onLoad: svgLoaded});

myWidth现在是144,因此包装器div的宽度不是svg(200px)中定义的宽度

svg.getElementById('fig');
那么你只需要做:

svg.getElementById('fig').getAttributeNS(null, 'width');
编辑:更完整的示例:

<!doctype html>
<html>
<head>
</head>
<body>

<embed src="rect01.svg" type="image/svg+xml"/>

<script>
    var emb, svg, w;

    emb = document.querySelector('embed');

    emb.addEventListener('load', function () {
      svg = emb.getSVGDocument().querySelector('#fig'); // also works with .documentElement
      w   = svg.getAttributeNS(null, 'width');
      console.log(w);
    });
</script>

</body>
</html>

var-emb,svg,w;
emb=document.querySelector('embed');
emb.addEventListener('load',函数(){
svg=emb.getSVGDocument().querySelector('#fig');//也适用于.documentElement
w=svg.getAttributeNS(null,'width');
控制台日志(w);
});

这将获取包装器div的宽度和高度,而不是另一个域上svg脚本的原始属性值?我更新了一个更完整的示例,这不是您想要的吗?谢谢bennedich,您的解决方案有效。我使用svg插件加载svg进行查询,如展开的问题所示。然后我得到div的宽度,而不是svg中的属性
svg.getElementById('fig').getAttributeNS(null, 'width');
<!doctype html>
<html>
<head>
</head>
<body>

<embed src="rect01.svg" type="image/svg+xml"/>

<script>
    var emb, svg, w;

    emb = document.querySelector('embed');

    emb.addEventListener('load', function () {
      svg = emb.getSVGDocument().querySelector('#fig'); // also works with .documentElement
      w   = svg.getAttributeNS(null, 'width');
      console.log(w);
    });
</script>

</body>
</html>