2010-05-02 95 views
65

我在搞SVG,希望能夠在Illustrator中創建SVG文件並使用Javascript訪問元素。如何使用Javascript訪問SVG元素

這裏的SVG文件的Illustrator踢了(這似乎也是垃圾的負載添加到文件的開頭,我已經刪除)

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> 
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" 
    width="276.843px" height="233.242px" viewBox="0 0 276.843 233.242" enable-background="new 0 0 276.843 233.242" 
    xml:space="preserve"> 
<path id="delta" fill="#231F20" d="M34.074,86.094L0,185.354l44.444,38.519l80.741-0.74l29.63-25.186l-26.667-37.037 
    c0,0-34.815-5.926-37.778-6.667s-13.333-28.889-13.333-28.889l7.407-18.519l31.111-2.963l5.926-21.481l-12.593-38.519l-43.704-5.185 
    L34.074,86.094z"/> 
<path id="cargo" fill="#DFB800" d="M68.148,32.761l43.704,4.445l14.815,42.963l-7.407,26.667l-33.333,2.963l-4.444,14.074 
    l54.074-1.481l22.222,36.296l25.926-3.704l25.926-54.074c0,0-19.259-47.408-21.481-47.408s-31.852-0.741-31.852-0.741 
    l-19.259-39.259L92.593,8.316L68.148,32.761z"/> 
<polygon id="beta" fill="#35FF1F" points="86.722,128.316 134.593,124.613 158.296,163.872 190.889,155.724 214.593,100.909 
    194.593,52.02 227.186,49.057 246.444,92.02 238.297,140.909 216.074,172.761 197.556,188.316 179.778,169.798 164.963,174.983 
    163.481,197.946 156.815,197.946 134.593,159.428 94.593,151.279 "/> 
<path class="monkey" id="alpha" fill="#FD00FF" d="M96.315,4.354l42.963,5.185l18.519,42.222l71.852-8.148l20.74,46.667l-5.926,52.593 
    l-24.444,34.074l-25.185,15.555l-14.074-19.259l-8.889,2.964l-1.481,22.222l-14.074,2.963l-25.186,22.963l-74.074,4.444 
    l101.481,4.444c0,0,96.297-17.777,109.63-71.852S282.24,53.983,250.389,20.65S96.315,4.354,96.315,4.354z"/> 
</svg> 

正如你可能會看到,每個元素都有一個ID,並且我希望能夠使用Javascript訪問各個元素,這樣我就可以更改填充屬性並響應諸如點擊等事件。

的HTML被沼澤基本

<!DOCTYPE html> 
<html> 
    <head> 
     <title>SVG Illustrator Test</title> 
    </head> 
    <body> 

     <object data="alpha.svg" type="image/svg+xml" id="alphasvg" width="100%" height="100%"></object> 

    </body> 
</html> 

我想這是兩個問題真的。

  1. 是否可以這樣做,而不是使用類似Raphael或jQuery SVG的東西。

  2. 如果可能,有什麼技術?


UPDATE

目前,我已經使出使用Illustrator創建SVG文件,我使用Raphaël JS創建路徑,並簡單地複製從點數據將SVG文件粘貼到path()函數中。 (據我所知)手動編寫點數據來創建地圖所​​需的複雜路徑非常複雜。

回答

82

是否可以這樣做,而不是使用像拉斐爾或jQuery SVG之類的東西?

肯定。

如果可能,有什麼技術?

這個註釋的代碼片斷工作:

<!DOCTYPE html> 
<html> 
    <head> 
     <title>SVG Illustrator Test</title> 
    </head> 
    <body> 

     <object data="alpha.svg" type="image/svg+xml" 
     id="alphasvg" width="100%" height="100%"></object> 

     <script> 
      var a = document.getElementById("alphasvg"); 

      // It's important to add an load event listener to the object, 
      // as it will load the svg doc asynchronously 
      a.addEventListener("load",function(){ 

       // get the inner DOM of alpha.svg 
       var svgDoc = a.contentDocument; 
       // get the inner element by id 
       var delta = svgDoc.getElementById("delta"); 
       // add behaviour 
       delta.addEventListener("mousedown",function(){ 
         alert('hello world!') 
       }, false); 
      }, false); 
     </script> 
    </body> 
</html> 

注意,這種技術的侷限性在於它是由同源策略的限制,所以alpha.svg必須在同一個域中.html主辦文件,否則該對象的內部DOM將無法訪問。

+3

關於@ jbeard4的說明:在某些瀏覽器中,此同源策略導致需要使用服務器(即使您的代碼僅使用html和js [serverside]),訪問策略將阻止訪問svg。 – Michael 2013-07-22 07:35:06

16

如果你使用jQuery,你需要等待$(window).load,因爲嵌入式SVG文件可能尚未加載在$(document).ready

$(window).load(function() { 

    //alert("Document loaded, including graphics and embedded documents (like SVG)"); 
    var a = document.getElementById("alphasvg"); 

    //get the inner DOM of alpha.svg 
    var svgDoc = a.contentDocument; 

    //get the inner element by id 
    var delta = svgDoc.getElementById("delta"); 
    delta.addEventListener("mousedown", function(){ alert('hello world!')}, false); 
}); 
+3

使用此代碼時,這個其他答案可能是一個有用的補充:http://stackoverflow.com/a/5990945/1515819 – 2013-02-24 21:08:03

+0

這onload tidbit挽救了我的生活。謝謝你 – Russ 2015-12-22 17:51:38

+0

在jQuery的情況下,你最好使用'$('#alphasvg').load'而不是'$(window).load'。這將允許您即時更改屬性「數據」 – vkabachenko 2018-02-24 11:23:00

1

如果您使用的是<img>標籤的SVG,那麼你不能操縱其內容(據我所知)。

正如接受的答案所示,使用<object>是一個選項。

我最近需要這一點,我大口的構建過程中使用gulp-inject到SVG文件的內容直接注入HTML文檔作爲<svg>元素,然後將其很容易使用CSS選擇器和querySelector/getElementBy*工作。

相關問題