描述: 运用面向对象的方法创建飞机对象实例
代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>面向对象创建飞机</title>
<style>
#box{
border: 1px solid #000;
width: 300px;
height: 500px;
position: relative;
}
</style>
</head>
<body>
<div id="box"></div>
<script>
var box = document.getElementById('box');
console.log(box);
function Plane(parent, node, src, x, y){
this.parent = parent;
this.node = node;
this.src = src;
this.x = x;
this.y = y;
this.img = null;
}
Plane.prototype.init = function(){
this.img = document.createElement(this.node);
console.log(this.img);
this.img.src = this.src;
this.img.style.position = 'absolute';
this.img.style.left = this.x + 'px';
this.img.style.top = this.y + 'px';
this.parent.appendChild(this.img);
}
var obj = new Plane(box, 'img', '../image/我的飞机.gif', 120, 120);
obj.init();
</script>
</body>
</html>