我不明白什麼是功能推的使用,它有什麼幫助。 1 - 爲什麼我需要行代碼?爲什麼我需要推送功能?
circles.push(newCircle);
2 - 我將此代碼複製到html文件,代碼沒有運行,我應該錯過這裏的東西嗎? THX
<html>
<head>
<title>Your title here</title>
<script type = "text/javascript" language = "Javascript">
<!-- Hide from older browsers;
var svgns = 'http://www.w3.org/2000/svg';
var svgElement = document.createElementNS(svgns, 'svg');
document.body.appendChild(svgElement);
var Circle = function(x,y,size){
this.element = document.createElementNS(svgns, 'circle');
this.x = x;
this.y = y;
this.size = size;
this.dx = 10*(Math.random()-0.5);
this.dy = 10*(Math.random()-0.5);
this.element.setAttribute('cx', this.x+'px');
this.element.setAttribute('cy', this.y+'px');
this.element.setAttribute('r', this.size+'px');
this.element.setAttribute('stroke', 'black');
this.element.setAttribute('stroke-width', '2px');
this.element.setAttribute('fill', 'red');
svgElement.appendChild(this.element);
};
Circle.prototype.update = function(){
this.x += this.dx;
this.y += this.dy;
this.element.setAttribute('cx', this.x+'px');
this.element.setAttribute('cy', this.y+'px');
};
var circles = [];
for (var i = 0; i< 10; i++) {
var newCircle = new Circle(100,100,10);
circles.push(newCircle);
}
window.setInterval(function(){
for (var i = 0; i< circles.length; i++) {
circles[i].update();
}
}, 30);
// end hide -->
</script>
</head>
<body>
<!-- Insert HTML here -->
</body>
</html>
看起來像你缺少你標籤的命名空間? – DdD 2012-03-13 22:23:11
實際上它的工作原理。 http://jsfiddle.net/eU32w/ – DdD 2012-03-13 22:25:32
@DimitriAdamou小提琴正在工作,因爲左上角的任何東西都包含在'
...'中。 – 2012-03-13 22:29:29