2012-01-03 67 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