2012-01-03 48 views
0

我与SVG-插件jquery的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加载加载外部SVG外部加载的SVG的属性,我可以得到BBOX或boundingClientRect但不是宽度(200像素)

var boxW = svg.getElementById('fig').getBBox().width; 
var bcrW = svg.getElementById('fig').getBoundingClientRect().width; 

我怎样才能得到SVG的属性值(用id =“无花果”)?

由于

的SVG装有SVG-jQuery插件现在

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

function svgLoaded(svg) { 
var myWidth = svg.getElementById('fig').getAttributeNS(null, 'width'); 

myWidth是144,因此,包装器-DIV的宽度不宽度定义在svg(200px)内

回答

0

如果你赶上#fig

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> 
+0

这得到宽度和包装div的高度原来不是值属性 – redon 2012-01-03 13:44:16

+0

是另一个域上的svg脚本? – bennedich 2012-01-03 14:13:44

+0

不,它在同一个域名 – redon 2012-01-03 15:00:26