2013-05-17 192 views
0

下面的代碼應該生成100個隨機矩形。但它不工作。任何人都可以告訴我我做錯了什麼?JavaScripts不支持SVG

<html> 
<head> 
<script type="text/javascript"> 
function rectan() 
{ 
var svgns = "http://www.w3.org/2000/svg"; 
for (var i = 0; i < 100; i++) { 
    var x = Math.random() * 5000, 
     y = Math.random() * 3000; 

    var rect = document.createElementNS(svgns, 'rect'); 
    rect.setAttributeNS(null, 'x', x); 
    rect.setAttributeNS(null, 'y', y); 
    rect.setAttributeNS(null, 'height', '50'); 
    rect.setAttributeNS(null, 'width', '50'); 
    rect.setAttributeNS(null, 'fill', '#'+Math.round(0xffffff * Math.random()).toString(16)); 
    document.getElementById('svgOne').appendChild(rect); 
} 
} 
</script> 
</head> 
<body onload="rectan"();"> 

<svg id="svgOne" xmlns="http://www.w3.org/2000/svg" width="5000" height="3000"> 
      <rect x="50" y="50" 
      width="50" height="50" 
      fill="black" 
      /> 
</svg> 
</body> 
</html> 

它所做的只是一個黑色矩形窗體SVG部分。我知道我在某個地方犯了一個錯誤,但我不知道在哪裏。

+0

它可能是在黑色的背景上繪製黑色,嘗試設置背景說紅看看你是否看到你的矩形。 – Tom

回答

2

onload屬性似乎存在無關的雙重qouote。你想要這個...

<body onload="rectan();"> 
+0

上帝我笨 - 我看到了這一點,但是當我試圖使這項工作我做了類似的東西 <身體的onload =「rectan」();> 和我RLY不知道爲什麼...求助THX伴侶 – Jake