<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>赛车游戏</title></head><body><!-- <img src="https://t7.baidu.com/it/u=2607436094,546726765&fm=193&f=GIF" height=100 width=100/> --><h1 id="test">特斯拉右移</h1><script src="https://code.jquery.com/jquery-3.6.1.js"></script><script>/* 这是面向对象的方法 */var Car=function(x,y){this.x=x;this.y=y;};// 创建绘制方法Car.prototype.draw=function(){var carHtml='<img src="https://t7.baidu.com/it/u=2607436094,546726765&fm=193&f=GIF" height=100 width=100/>';this.carElement=$(carHtml);this.carElement.css({position:"absolute",left:this.x,top:this.y})$("body").append(this.carElement);};// 创建右移方法Car.prototype.moveRight=function(){this.x+=100;this.carElement.css({left:this.x,top:this.y})};var tesla=new Car(20,65);var nissan=new Car(100,180);tesla.draw();nissan.draw();new Car(190,330).draw();$("#test").click(function(){tesla.moveRight();});/* 以下是非面向对象的方法 */// var Car=function(x,y){// this.x=x;// this.y=y;// // drawCar(this);// }// var drawCar=function(car){// var carHtml='<img src="https://t7.baidu.com/it/u=2607436094,546726765&fm=193&f=GIF" height=100 width=100/>';// var carElement=$(carHtml);// carElement.css({// position:"absolute",// left:car.x,// top:car.y,// });// $("body").append(carElement);// }// var tesla=new Car(20,20);// var nissan=new Car(100,200)// drawCar(tesla);// drawCar(nissan);</script></body>
</html>