2013-05-17 121 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