js基础3

2018-08-21  本文已影响0人  HavenYoung

一、添加事件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>添加事件</title>
</head>
<body>
    <div style="width:200px; height:200px; background-color:red" onclick="alert('这是一个div')"></div>
    <div style="width:200px; height:200px; background-color:blue" onclick="test()"></div>
    <div id="div" style="width:200px; height:200px; background-color:green"></div>
</body>
</html>
<script>
function test() {
    console.log('花田里犯了错,说好,破晓前忘掉')
}
var odiv = document.getElementById('div')
// odiv.onclick = function () {
//     console.log('遥远的东方有一条龙')
// }
odiv.onclick = test
</script>

二、onload函数

三、选项卡

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>选项卡</title>
    <style>
    button {
        width: 200px;
        height: 100px;
        font-size: 40px;
        background-color: pink;
        margin-left: 50px;
        display: inline-block;
    }
    div {
        width: 970px;
        height: 600px;
        font-size: 50px;
        background-color: yellow;
        margin-left: 50px;
        margin-top: 50px;
        display: none;
    }
    .active {
        font-size: 50px;
        background-color: blue;
    }
    .show {
        display: block;
    }
    </style>
</head>
<body>
    <button class="active" onclick="dudu(this, 0)">周杰伦</button>
    <button onclick="dudu(this, 1)">王力宏</button>
    <button onclick="dudu(this, 2)">张学友</button>
    <button onclick="dudu(this, 3)">刘德华</button>
    <div class="show">菊花台、千里之外、七里香、霍元甲、听妈妈的话、稻香、双节棍、简单爱</div>
    <div>花田错、龙的传人、唯一</div>
    <div>慢慢、吻别、一千个伤心的理由</div>
    <div>谢谢你的爱、冰雨、天意、忘情水</div>
</body>
</html>
<script>
    // 得到所有的button
    var abuttons = document.getElementsByTagName('button')
    // 得到所有的div
    var adivs = document.getElementsByTagName('div')
    function dudu(obj, index) {
        // 先将所有的button的class属性设置为空
        for (var i = 0; i < abuttons.length; i++) {
            abuttons[i].className = ''
            adivs[i].className = ''
        }
        // 给指定的button添加样式
        obj.className = 'active'
        // 给指定的div添加样式
        adivs[index].className = 'show'
    }
</script>

四、定时器

定时器:分为两种,一种是周期性定时器,一种是一次性定时器

五、获取非行内样式

六、BOM操作

七、DOM操作

八、select下拉框和oninput事件

上一篇 下一篇

猜你喜欢

热点阅读