js this 点击事件 for循环 案例1

点击左边的盒子,盒子变成黄色,右边的上下盒子显示相应的字母,鼠标移入下面盒子,背景变色
我比较习惯上传复制粘贴就可以运行的代码
在这里插入图片描述

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./2.css">
</head>

<body>
    <div class="wrap">
        <ul class="left">
            <li>a</li>
            <li>b</li>
            <li>c</li>
            <li>d</li>
        </ul>
        <div class="right">
            <div class="line1">a1</div>
            <div>
                <p>a1</p>
                <p>a2</p>
                <p>a3</p>
                <p>a4</p>
                
            </div>
        </div>
    </div>
    <script>
        var ul = document.getElementsByTagName('ul');
        var lis = document.getElementsByTagName('li');
        var line = document.getElementsByClassName('line1')[0];
        var p = document.getElementsByTagName('p');

        //点击a的时候右边的全部显示它的内容,且a1是白色背景
        for (var i = 0; i < lis.length; i++) {
            lis[i].onmouseenter = function () {
                //排他 一定加排他的for 循环 不能用别人的   左边盒子的
                for (var i = 0; i < lis.length; i++) {
                    lis[i].style.background = '';
                }
                this.style.background = 'gold';
                for (var j = 0; j < p.length; j++) {
                    line.innerText = this.innerText + 1;
                    console.log(line.innerText);
                    p[j].innerText = this.innerText + (j + 1); 
                    p[0].style.background = '#fff';
                    p[j].onmouseenter = function () {
                        //排他 一定加排他的for 循环 不能用别人的  右边横着的盒子的
                        for (var j = 0; j < p.length; j++) {
                            p[j].style.background = '';
                        }
                        this.style.background = '#fff';

                    }
                    console.log(p[j].innerText);
                }

            }

        }

    </script>
</body>

</html>
*{
    padding: 0;
    margin: 0;
}
ul{
    list-style-type: none;
}
.wrap{
    width: 600px;
    height: 400px;
    background-color: pink;
    display: flex;
    margin: 20px auto;
}
.left{
    width: 150px;
    height: 400px;
}
.left li{
    width: 150px;
    height: 100px;
    text-align: center;
    line-height: 100px;
} 
.left li:nth-child(1){
    background-color: red;
}
.left li:nth-child(2){
    background-color: green;
}
.left li:nth-child(3){
    background-color: pink;
}
.left li:nth-child(4){
    background-color: orangered;
}
.right {
    width: 450px;
    height: 400px;
   
}
.right div:nth-child(1){
    width: 450px;
    height: 350px;
    background: skyblue;
    text-align: center;
    line-height: 300px;
}
.right div:nth-child(2) {
    width: 450px;
    height: 500px;
   display: flex;
   justify-content: space-between;
}
.right div:nth-child(2) p{
    width: 110px;
    height: 50px;
    background: purple;
    text-align: center;
    line-height: 50px;
}
.right div:nth-child(2) p{
    width: 110px;
    height: 50px;
    background: purple;
    text-align: center;
    line-height: 50px;
}



版权声明:本文为weixin_41684068原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。