2011-05-15 44 views
0

在jQuery中SVG插件(http://plugins.jquery.com/project/svg)與SVG工作執行從DIV元素開始和創建div中新的SVG對象:SVG通過jQuery.svg

Attach an SVG canvas to a with the following:

$(selector).svg(); 

是否有可能與連接jquery到現有的svg對象?如果「是」如何做到這一點?

編輯: 司機小費

<body onload="alert('loaded');testSvg();"> 
    <div id="div3" style="border: solid 1px black; width:200px; height:100px"> 
     <svg id="svg3" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"></svg> 
    </div> 

    <script type="text/javascript"> 
    function testSvg() { 
     var svgRootElement = $("#svg3").get(); 
     var svgWrapper = new $.svg._wrapperClass(svgRootElement); 
     svgWrapper.circle(30, 25, 50, { fill: 'red' }); 
    } 

導致錯誤:

'TypeError: Unable to get value of the property 'createElementNS': object is null or undefined' error...

爲什麼呢?

+0

我建議你出打破編輯成新問題。我對如何回答這個問題有了一個想法,但是在這裏回答它會太混亂。 – jbeard4 2011-05-15 21:14:33

+0

完成:http://stackoverflow.com/questions/6033939/how-to-create-jquery-wrapper-on-existing-svg-object – Budda 2011-05-17 16:19:15

回答

1

是的,這是可能的。它被描述爲here,雖然有點難以找到。如果您使用內聯SVG(例如,你使用的不是對象標記,或者一個iframe嵌入SVG到HTML),那麼下面應該工作:

$(document).ready(function(){ 
var svgRootElement = $("#myExistingSVGObject"); 
var svgWrapper = new $.svg._wrapperClass(svgRootElement); // this is the equivalent of what is returned from the call to .svg() 
}) 
+0

它是否需要在'準備好'功能?我可以在身體部分的末尾做同樣的事嗎?我試過了,似乎不太可行... – Budda 2011-05-15 16:51:26

+0

是的,你可以把它放在正文部分末尾的腳本元素中。因爲元素以文檔順序加載,所以您必須執行此操作,或將其包裝在文檔就緒回調中。通常情況下,最好將腳本放在head元素中,這就是爲什麼大多數JavaScript庫包含一個用於監聽dom ready事件的跨瀏覽器解決方案的原因。儘管如此,無論哪種方式都應該可行 – jbeard4 2011-05-15 17:03:39

+0

問題:'svg()'返回了什麼?我只使用'svg'變量,它被傳遞到.svg({onLoad:MyMethod})中使用的'onLoad'方法; ...如何使用'clean'svg()? – Budda 2011-05-15 18:13:51