2014-09-19 90 views
0

我想在IE9中使用javascript創建svg,所以我使用了document.craeteElementNS方法。它適用於其他瀏覽器,但不是IE9。我能知道爲什麼嗎?這裏是一個文件,說明該方法應該與IE9, http://msdn.microsoft.com/en-us/library/ie/ff975213%28v=vs.85%29.aspx 一起工作但是當我嘗試了它,它不,我可以知道爲什麼嗎?
Result: It doesn't shows the polygon on the page that it should be, it just convey a blank pagedocument.createElementNS方法不能在IE9上工作

<html> 
    <head> 
    <script src="http://code.jquery.com/jquery-1.10.2.js"></script> 
    <script> 
    $(window).load(function(){ 
     var myDiv = document.getElementById("myDiv"); 
     var mySVG = document.createElementNS("http://www.w3.org/2000/svg", "svg"); 
     mySVG.setAttribute("height","210"); 
     mySVG.setAttribute("width","500") 
     myDiv.appendChild(mySVG); 
     var myPolygon = document.createElementNS("http://www.w3.org/2000/svg", "polygon"); 
     myPolygon.setAttribute("style","fill:lime;stroke:purple;stroke-width:1"); 
     myPolygon.setAttribute("points","200,10 250,190 160,210"); 
     mySVG.appendChild(myPolygon); 
     });   
    </script> 
    </head> 
    <body> 
    <div id="myDiv"> 

    </div> 

    </body> 
    </html> 
+1

究竟 「不工作」?你有錯誤嗎?有什麼問題? 「它不起作用」是最糟糕的錯誤描述,因爲它沒有傳達任何信息。你的代碼不起作用已經暗示你在這裏發佈。 – 2014-09-19 17:30:40

+0

它不顯示多邊形,頁面上沒有任何內容。對不起,缺少信息 – dramasea 2014-09-19 17:33:30

+0

您是如何解決問題的? – 2014-09-19 17:34:16

回答

-3

$(document).ready(function(){ 
 
    var myDiv = document.getElementById("myDiv"); 
 
     var mySVG = document.createElementNS("http://www.w3.org/2000/svg", "svg"); 
 
     mySVG.setAttribute("height","210"); 
 
     mySVG.setAttribute("width","500"); 
 
     myDiv.appendChild(mySVG); 
 
    var obj = document.createElementNS("http://www.w3.org/2000/svg", "polygon"); 
 
    //obj.setAttributeNS(null, "cx", 100); 
 
    //obj.setAttributeNS(null, "cy", 50); 
 
    //obj.setAttributeNS(null, "r", 40); 
 
    obj.setAttributeNS(null, "stroke", "purple"); 
 
    obj.setAttributeNS(null, "stroke-width", 1); 
 
    obj.setAttributeNS(null, "fill", "lime"); 
 
    obj.setAttributeNS(null, "points", "200,10 250,190 160,210"); 
 
    mySVG.appendChild(obj); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> 
 
<div id="myDiv"></div>

+2

爲什麼你玩「點差異」遊戲,而不是提供一些有用的解釋?你發佈的代碼有什麼問題?它如何解決OP的問題? – 2014-09-19 22:52:42