js面向对象和事件

// 面向对象

 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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值