JavaScript学习三(js dom常用事件操作方法)

Dom鼠标事件

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .btn {
            width: 140px;
            height: 50px;
            background: red;
            line-height: 50px;
            font-size: 18px;
            text-align: center;
            margin-top: 30px;
            cursor: pointer;
            color: blue;
            border-radius: 5px;
        }

        .btn2 {
            width: 140px;
            height: 50px;
            background: blue;
            line-height: 50px;
            font-size: 18px;
            text-align: center;
            margin-top: 30px;
            cursor: pointer;
            color: red;
            border-radius: 5px;
        }
    </style>
</head>
<body>
<input type="button" value="弹出" onclick="alert('我是按钮')"/>
<div id="btn" class="btn" onmousemove="mouseoverFn(this)" onmouseout="mouseoutFn(this)">按钮</div>
<script>
    function mouseoverFn(btn) {
        btn.className = "btn2"
    }

    function mouseoutFn(btn) {
        btn.className = "btn"
    }
</script>
</body>

</html>

鼠标放上

DOM 0级事件

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .btn {
            width: 140px;
            height: 50px;
            background: red;
            line-height: 50px;
            font-size: 18px;
            text-align: center;
            margin-top: 30px;
            cursor: pointer;
            color: blue;
            border-radius: 5px;
        }

        .btn2 {
            width: 140px;
            height: 50px;
            background: blue;
            line-height: 50px;
            font-size: 18px;
            text-align: center;
            margin-top: 30px;
            cursor: pointer;
            color: red;
            border-radius: 5px;
        }
    </style>
</head>
<body>
<div id="btn" class="btn">开锁</div>
</body>
<script>
    /**
     * dom 0 级事件 指的就是 先取出dom元素再指定事件,而不是现在标签内部
     */
    var btn = document.getElementById("btn");
    btn.onclick = function () {
        if (this.innerHTML == "开锁") {
            this.className = "btn2";
            this.innerHTML = "上锁"
        } else {
            this.className = "btn";
            this.innerHTML = "开锁"
        }
    }
</script>
</html>

鼠标点击按钮后

window.onload()事件

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<script>
    /**
     * 在页面加载完以后执行 会先执行后面的 执行完毕后再执行onload        里面的事件
     */
    window.onload = function () {
        var box = document.getElementById("box");
        box.onclick = function () {
            alert("这是一个DIV");
        }
    }
</script>
<body>
<div id="box">div</div>
</body>
</html>



 

 

onchange()事件

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body id="bgChange">
<div style="font-size: 40px;">
    <label>请选择你想要的背景色</label>
    <select id="selectColor" style="font-size: 30px;">
        <option>请选择</option>
        <option value="红色">红色</option>
        <option value="黄色">黄色</option>
        <option value="蓝色">蓝色</option>
        <option value="黑色">黑色</option>
        <option value="绿色">绿色</option>
    </select>
</div>

<script>
    var bg = document.getElementById("bgChange");
    var selectColor = document.getElementById("selectColor");
    selectColor.onchange = function () {
//				var bgColor = this.value;
        var bgColor = this.options[this.selectedIndex].value;
        console.log(bgColor);
        if (this.value == '红色')
            bg.style.backgroundColor = 'red';
        if (this.value == '黄色')
            bg.style.backgroundColor = 'yellow';
        if (this.value == '蓝色')
            bg.style.backgroundColor = 'blue';
        if (this.value == '黑色')
            bg.style.backgroundColor = 'black';
        if (this.value == '绿色')
            bg.style.backgroundColor = 'green';
    }
</script>
</body>
</html>

 

表单获得焦点onfocus() 失去焦点事件 onblur()

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .box {
            padding-left: 50px;
        }

        .left, .tips {
            float: left;
        }

        .left {
            padding-right: 20px;
        }

        .tips {
            display: none;
        }
    </style>
    <script>
        window.onload = function () {
            var telephone = document.getElementById("telephone");
            var tip = document.getElementById("tip");
            telephone.onfocus = function () {
                tip.style.display = 'block';
            }
            telephone.onblur = function () {
                if (this.value.length == 11 && isNaN(this.value) == false) {
                    tip.innerHTML = '√'
                } else {
                    tip.innerHTML = '×'
                }
            }
        }
    </script>
</head>
<body>
<div id="box" class="box">
    <div class="left">
        手机号码:<input type="text" id="telephone" placeholder="请输入手机号码"/>
    </div>
    <div class="tips" id="tip">
        请输入手机号
    </div>
</div>
</body>
</html>


 

键盘事件

定时事件

 

 

 

dom节点

添加追加子节点

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="div">
    <p id="p1">段落1</p>
    <p id="p2">段落2</p>
</div>
<button id="button1">按钮</button>
<script>
    var button1 = document.getElementById("button1");
    var p1 = document.getElementById("p1");
    var p2 = document.getElementById("p2");
    var div = document.getElementById("div");
    button1.onclick = function () {
        var p3 = document.createElement("p");
        var text = document.createTextNode("段落3");
        p3.appendChild(text);
        div.appendChild(p3);
    }
</script>
</body>
</html>

点击后

 

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值