// 面向对象
var car={
category:"跑车",
color:"red",
price:200,
brand:"拖拉机",
drive:function(){
alert(this.brand+"正在行驶");
},
light:function(){
alert(this.brand+"打开了灯");
}}
alert(car.color);
car.drive();
// 面向对象二
var car2=new Object();
car2.color="red";
car2.brand="拖拉机";
car2.drive=function(){
alert(this.brand+"正在行驶2");
}
alert(car2.color);
car2.drive();
// 面向对象3
function car_3(color,brand){
this.color=color;
this.brand=brand;
this.drive=function(){
alert(this.brand+"正在行驶3");
}
}
var car3=new car_3("red","小破车");
alert(car3.color);
car3.drive();
js事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="button" id="btn_1" value="新增元素">
<input type="input_1" id="input_1" value="新增元素">
</body>
<script>
function addElement(){
//实现向页面中添加元素
//实现功能
var box=document.createElement("img");
box.style.width="100px";
box.style.height="100px";
box.style.backgroundColor="#bfa";
box.style.position="absolute";
box.style.left=Math.random()*1000+"px";
box.style.top=Math.random()*600+"px";
box.src="./logo.png"
var body=document.body;
body.appendChild(box);
}
//获取事件源对象,按钮
var button=document.querySelector("#btn_1");
var input_1=document.querySelector("#input_1");
//绑定事件,一个元素只能绑一个,否则会覆盖
button.οnclick=function(){
alert("绑定成功");
addElement();
}
input_1.οnfοcus=function(){
alert("绑定成功");
addElement();
}
//绑定事件,一个元素只能绑一个,否则会覆盖
// button.οnclick=function(){
// alert("绑定事件2");
// }
//取消绑定
// button.οnclick=false;
// button.addEventListener("click",addElement);
// button.addEventListener("click",function(){
// alert("绑定成功");
// })
// //添加事件监听器
// button.addEventListener("click",addElement);
</script>
</html>