js this用法 进度条

本文展示了JavaScript如何与HTML元素交互,包括计算、事件处理、样式修改等。通过点击事件改变元素内容,动态计算加减乘除及百分比,并实现进度条效果。此外,还涉及this关键字的理解及其在不同上下文中的应用。
摘要由CSDN通过智能技术生成
<!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>
    <style>
        
        input{
            vertical-align: middle;
            height: 30px;
            padding: 0;
        }
        span{
            display: inline-block;
            width: 30px;
            height: 30px;
            border: 1px dashed yellowgreen;
            vertical-align: middle;
            font: 20px/30px "";
            text-align: center;
        }
        p{
            width: 200px;
            height: 30px;
            border: 2px solid orange;
            vertical-align: middle;
            display: inline-block;
            background: #8df;
        }
        div{
            display: inline-block;
            width: 30px;
            height: 30px;
            font: 20px/30px "";
            text-align: center;
            border: 1px solid #000;
            background: #ccc;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <input type="text" placeholder="输入数字">
    <span></span>
    <input type="text" placeholder="输入数字">
    <span style="cursor: pointer;">=</span>
    <p></p>
    <br>
    <div>+</div>
    <div>-</div>
    <div>*</div>
    <div>/</div>
    <div>%</div>


    <script>
        // 获取元素节点
        var aInput = document.querySelectorAll('input');
        console.log(aInput);
        var aSpan = document.querySelectorAll('span');
        console.log(aSpan);
        var oP = document.querySelector('p');
        console.log(oP);
        var aDiv = document.querySelectorAll('div');
        console.log(aDiv);


        // 选择 运算符
            // aDiv[0].onclick = function(){
            //     aSpan[0].innerHTML = aDiv[0].innerHTML;
            // }

            // aDiv[1].onclick = function(){
            //     aSpan[0].innerHTML = aDiv[1].innerHTML;
            // }

            // aDiv[2].onclick = function(){
            //     aSpan[0].innerHTML = aDiv[2].innerHTML;
            // }

            // aDiv[3].onclick = function(){
            //     aSpan[0].innerHTML = aDiv[3].innerHTML;
            // }

            // aDiv[4].onclick = function(){
            //     aSpan[0].innerHTML = aDiv[4].innerHTML;
            // }


                       // 5
        // for(var i=0;i<aDiv.length;i++){
        //     console.log(aDiv[i]);

        //     aDiv[i].onclick = function(){
        //         // console.log(aSpan[0])
        //         // console.log(aDiv[i]);
        //         // console.log(i);  // 5
        //         aSpan[0].innerHTML = aDiv[i].innerHTML;
        //     }
        // }
        // console.log(i);  // 5


        for(var i=0;i<aDiv.length;i++){
            console.log(aDiv[i]);

            aDiv[i].onclick = function(){
                // console.log(this);
                aSpan[0].innerHTML = this.innerHTML;
            }
        }
      
        // 计算结果
        aSpan[1].onclick = function(){
            
            // 获取 两个输入框的值
            var num1 = aInput[0].value;
            console.log(num1);
            var num2 = aInput[1].value;
            console.log(num2);

            var ysf = aSpan[0].innerHTML;
            console.log(ysf);

            if(num1==="" || num2===""){
                alert('没有输入数字,请输入后计算')
            }else if(ysf === ''){
                alert('请选择运算符后计算')
            }else if(ysf === '+'){
                oP.innerHTML =  +num1 + +num2;
            }else if(ysf === '-'){
                oP.innerHTML =  num1 - num2;
            }else if(ysf === '*'){
                oP.innerHTML =  num1 * num2;
            }else if(ysf === '/'){
                oP.innerHTML =  num1 / num2;
            }else if(ysf === '%'){
                oP.innerHTML =  num1 % num2;
            }


            aInput[0].value = '';
            aInput[1].value = '';
            aSpan[0].innerHTML = '';
        }
    </script>
</body>
</html>
<!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>
    <style>
        #box{
            width: 200px;
            height: 200px;
            background: yellow;
        }
    </style>
</head>
<body>
    <div id="box"></div>

    <script>
        // this
            // this是js中的一个关键字,表示 这个
            // 这个 是谁呢?
            // 我们可以在任何this 访问 this
                // 全局中 访问 this,表示的是 window对象
                // 函数中 访问 this, 表示的是 函数的调用者--对象
            // 所以 this在 不同情况下 表示的是 不同的对象 --- this指向


        // this指向 ---- this到底是谁
            // 全局中的 this 指向 window对象
            // 普通函数中的 this 指向 window对象
            // 事件处理函数中的 this 指向 事件源
            // 定时器中的this 指向 window对象
            // 对象方法中的this 指向 对象本身

        // 全局的this
        console.log(this);   // window对象


        // 普通函数中的this
        function fn1(){
            console.log(this);  // window对象
        }

            fn1();
            // window.fn1();
            // this.fn1();


        // 事件处理函数中的this
        document.querySelector('#box').onclick = function(){
            console.log(this);  // 事件源
            // this.style.border = '20px double blue';
        }


        //  定时器中的this
        setInterval(function(){
            console.log(this);  // window对象
        },3000);



        // 对象方法中的this
        var obj = {
            name: 'haha',
            age: 18,
            hello: function(){
                console.log(this);  // 方法所属的对象--对象本身
                console.log(this.age);  // 18
            }
        }

        obj.hello();
    </script>
</body>
</html>
<!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>
    <style>
        #box{
            height: 30px;
            border: 5px groove orange;
            border-radius: 20px;
            position: relative;
        }
        #box p{
            margin: 0;
            width: 0;
            height: 30px;
            background: pink;
            border-radius: 15px;
        }
        #box span{
            width: 100px;
            height: 30px;
            background: rgba(0,0,0,.3);
            text-align: center;
            font: 20px/30px "楷体";
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            margin: auto;
        }
    </style>
</head>
<body>
    <div id="box" style="width: 800px;">
        <p></p>
        <span>0%</span>
    </div>

    <script src="./../../../public.js"></script>
    <script>
        // 获取元素节点
        var oBox = document.querySelector('#box');
        console.log(oBox);
        var oChild = document.querySelector('#box>p');
        console.log(oChild);
        var oBili = document.querySelector('#box>span');
        console.log(oBili);

        // 得到 父元素的宽度  并 去掉px单位
        // var oBoxWidth = parseInt(oBox.style.width);
        var oBoxWidth = +oBox.style.width.slice(0,-2);
        console.log(oBoxWidth);

        // 定义 宽度的 初始值
        var num = 0;

        // 定义定时器
        var timer = setInterval(function(){
            // 更改 宽度 初始值
            num += randomNum(1,5);

            // 给 子元素p 设置宽度
            oChild.style.width = num+'px';

            // 设置 显示百分比
            oBili.innerHTML = (num/oBoxWidth*100).toFixed(2) + '%';

            // 判断 子元素p的宽度 是否 父元素一样了或者超过了
            if(num>=oBoxWidth){
                // 停止定时器
                clearInterval(timer);

                // 强行 设置 宽度初始值为 和 父元素一样的值
                num = oBoxWidth;

                // 因为 有可能 子元素p的宽度 超过 父元素,强制拉回来
                oChild.style.width = num+'px';
                // 因为 有可能 子元素p的宽度 超过 父元素 所以 通过 num参与计算的值就可能超100%,强行设置百分百
                oBili.innerHTML = num/oBoxWidth*100 + '%';
            }

        },50)
        
        





    </script>
</body>
</html>
<!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>
    <style>
        #box{
            height: 30px;
            border: 5px groove orange;
            border-radius: 20px;
            position: relative;
        }
        #box p{
            margin: 0;
            width: 0;
            height: 30px;
            background: pink;
            border-radius: 15px;
        }
        #box span{
            width: 100px;
            height: 30px;
            background: rgba(0,0,0,.3);
            text-align: center;
            font: 20px/30px "楷体";
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            margin: auto;
        }
    </style>
</head>
<body>
    <div id="box" style="width: 800px;">
        <p></p>
        <span>0%</span>
    </div>
    <button id="start">开始</button>

    <script src="./../../../public.js"></script>
    <script>
        // 获取元素节点
        var oBox = document.querySelector('#box');
        console.log(oBox);
        var oChild = document.querySelector('#box>p');
        console.log(oChild);
        var oBili = document.querySelector('#box>span');
        console.log(oBili);
        var oStart = document.querySelector('#start');
        console.log(oStart);

        // 得到 父元素的宽度  并 去掉px单位
        // var oBoxWidth = parseInt(oBox.style.width);
        var oBoxWidth = +oBox.style.width.slice(0,-2);
        console.log(oBoxWidth);

        // 定义 宽度的 初始值
        var num = 0;

        // 接收 定时器的返回值
        var timer = null;


        // 给 开始按钮 绑定 单击事件
        oStart.onclick = function(){
            
            // 停止定时器 --- 停止 上一个定时器  ---- 防抖
            clearInterval(timer);

            // 开启定时器
            timer = setInterval(function(){
                // 更改 宽度 初始值
                num += randomNum(1,5);

                // 给 子元素p 设置宽度
                oChild.style.width = num+'px';

                // 设置 显示百分比
                oBili.innerHTML = (num/oBoxWidth*100).toFixed(2) + '%';

                // 判断 子元素p的宽度 是否 父元素一样了或者超过了
                if(num>=oBoxWidth){
                    // 停止定时器
                    clearInterval(timer);

                    // 强行 设置 宽度初始值为 和 父元素一样的值
                    num = oBoxWidth;

                    // 因为 有可能 子元素p的宽度 超过 父元素,强制拉回来
                    oChild.style.width = num+'px';
                    // 因为 有可能 子元素p的宽度 超过 父元素 所以 通过 num参与计算的值就可能超100%,强行设置百分百
                    oBili.innerHTML = num/oBoxWidth*100 + '%';
                }
            },50)
        }

    </script>
</body>
</html>
<!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>
    <style>
        #box{
            height: 30px;
            border: 5px groove orange;
            border-radius: 20px;
            position: relative;
        }
        #box p{
            margin: 0;
            width: 0;
            height: 30px;
            background: pink;
            border-radius: 15px;
        }
        #box span{
            width: 100px;
            height: 30px;
            background: rgba(0,0,0,.3);
            text-align: center;
            font: 20px/30px "楷体";
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            margin: auto;
        }
    </style>
</head>
<body>
    <div id="box" style="width: 800px;">
        <p></p>
        <span>0%</span>
    </div>
    <button id="start">开始</button>
    <button id="stop">暂停</button>

    <script src="./../../../public.js"></script>
    <script>
        // 获取元素节点
        var oBox = document.querySelector('#box');
        console.log(oBox);
        var oChild = document.querySelector('#box>p');
        console.log(oChild);
        var oBili = document.querySelector('#box>span');
        console.log(oBili);
        var oStart = document.querySelector('#start');
        console.log(oStart);
        var oStop = document.querySelector('#stop');
        console.log(oStop);

        // 得到 父元素的宽度  并 去掉px单位
        // var oBoxWidth = parseInt(oBox.style.width);
        var oBoxWidth = +oBox.style.width.slice(0,-2);
        console.log(oBoxWidth);

        // 定义 宽度的 初始值
        var num = 0;

        // 接收 定时器的返回值
        var timer = null;

        // 设置 暂停按钮 禁用
            // 元素节点.disabled = true/false
                // true 禁用
                // false 不禁用
        oStop.disabled = true;

        // 给 开始按钮 绑定 单击事件
        oStart.onclick = function(){
            // 停止定时器 --- 停止 上一个定时器  ---- 防抖
            clearInterval(timer);

            // 解除 暂停按钮的 禁用  ---- 不禁用
            oStop.disabled = false;

            // 设置 开始按钮被禁用
            this.disabled = true;

            // 开启定时器
            timer = setInterval(function(){
                // 更改 宽度 初始值
                num += randomNum(1,5);

                // 给 子元素p 设置宽度
                oChild.style.width = num+'px';

                // 设置 显示百分比
                oBili.innerHTML = (num/oBoxWidth*100).toFixed(2) + '%';

                // 判断 子元素p的宽度 是否 父元素一样了或者超过了
                if(num>=oBoxWidth){
                    // 停止定时器
                    clearInterval(timer);

                    // 强行 设置 宽度初始值为 和 父元素一样的值
                    num = oBoxWidth;

                    // 因为 有可能 子元素p的宽度 超过 父元素,强制拉回来
                    oChild.style.width = num+'px';
                    // 因为 有可能 子元素p的宽度 超过 父元素 所以 通过 num参与计算的值就可能超100%,强行设置百分百
                    oBili.innerHTML = num/oBoxWidth*100 + '%';

                    // 设置 暂停按钮被禁用 
                    oStop.disabled = true;
                }
            },50)
        }


        // 给 暂定按钮 绑定 单击事件
        oStop.onclick = function(){
            // 停止定时器
            clearInterval(timer);

            // 设置 暂停按钮被禁用 
            this.disabled = true;

            // 解除 开始按钮的禁用 ---- 不禁用
            oStart.disabled = false;
        }
    </script>
</body>
</html>
<!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>
    <style>
        #box{
            height: 30px;
            border: 5px groove orange;
            border-radius: 20px;
            position: relative;
        }
        #box p{
            margin: 0;
            width: 0;
            height: 30px;
            background: pink;
            border-radius: 15px;
        }
        #box span{
            width: 100px;
            height: 30px;
            background: rgba(0,0,0,.3);
            text-align: center;
            font: 20px/30px "楷体";
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            margin: auto;
        }
    </style>
</head>
<body>
    <div id="box" style="width: 800px;">
        <p></p>
        <span>0%</span>
    </div>
    <button id="start">开始</button>
    <button id="stop">暂停</button>
    <button id="reset">重置</button>

    <script src="./../../../public.js"></script>
    <script>
        // 获取元素节点
        var oBox = document.querySelector('#box');
        console.log(oBox);
        var oChild = document.querySelector('#box>p');
        console.log(oChild);
        var oBili = document.querySelector('#box>span');
        console.log(oBili);
        var oStart = document.querySelector('#start');
        console.log(oStart);
        var oStop = document.querySelector('#stop');
        console.log(oStop);
        var oReset = document.querySelector('#reset');
        console.log(oReset);

        // 得到 父元素的宽度  并 去掉px单位
        // var oBoxWidth = parseInt(oBox.style.width);
        var oBoxWidth = +oBox.style.width.slice(0,-2);
        console.log(oBoxWidth);

        // 定义 宽度的 初始值
        var num = 0;

        // 接收 定时器的返回值
        var timer = null;

        // 设置 暂停 和 重置 按钮 禁用
            // 元素节点.disabled = true/false
                // true 禁用
                // false 不禁用
        oStop.disabled = true;
        oReset.disabled = true;

        // 给 开始按钮 绑定 单击事件
        oStart.onclick = function(){
            // 停止定时器 --- 停止 上一个定时器  ---- 防抖
            clearInterval(timer);

            // 解除 暂停 和 重置 按钮的 禁用  ---- 不禁用
            oStop.disabled = false;
            oReset.disabled = false;

            // 设置 开始按钮被禁用
            this.disabled = true;
            

            // 开启定时器
            timer = setInterval(function(){
                // 更改 宽度 初始值
                num += randomNum(1,5);

                // 给 子元素p 设置宽度
                oChild.style.width = num+'px';

                // 设置 显示百分比
                oBili.innerHTML = (num/oBoxWidth*100).toFixed(2) + '%';

                // 判断 子元素p的宽度 是否 父元素一样了或者超过了
                if(num>=oBoxWidth){
                    // 停止定时器
                    clearInterval(timer);

                    // 强行 设置 宽度初始值为 和 父元素一样的值
                    num = oBoxWidth;

                    // 因为 有可能 子元素p的宽度 超过 父元素,强制拉回来
                    oChild.style.width = num+'px';
                    // 因为 有可能 子元素p的宽度 超过 父元素 所以 通过 num参与计算的值就可能超100%,强行设置百分百
                    oBili.innerHTML = num/oBoxWidth*100 + '%';

                    // 设置 暂停按钮被禁用 
                    oStop.disabled = true;
                }
            },50)
        }


        // 给 暂定按钮 绑定 单击事件
        oStop.onclick = function(){
            // 停止定时器
            clearInterval(timer);

            // 设置 暂停按钮被禁用 
            this.disabled = true;

            // 解除 开始按钮的禁用 ---- 不禁用
            oStart.disabled = false;
        }


        // 给 重置按钮 绑定 单击事件
        oReset.onclick = function(){
            // 刷新网页,但是这个对逻辑没有锻炼,我们就不用这个了
            // location.reload();

            // 设置 重置 和 暂停 按钮  禁用
            this.disabled = true;
            oStop.disabled = true;
            // 设置 开始 按钮 不禁用
            oStart.disabled = false;

            // 停止定时器
            clearInterval(timer);

            // 设置 初始宽度的值为 0
            num = 0;

            // 设置 子元素p 的 宽度为 0px
            oChild.style.width = '0px';

            // 设置 子元素span 的 内容为 0%
            oBili.innerHTML = '0%';
        }
    </script>
</body>
</html>
<!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>
    <style>
        .box{
            width: 400px;
            border: 10px double pink;
            background: orange;
        }
    </style>
</head>
<body>
    <div class="box">
        hello world!!!
        <p>pbox</p>
        <em>hahah</em>
        <span>lala</span>
        <nav>navbox</nav>
    </div>

    <header style="height: 50px;background: yellow">header</header>



    <script>
        // 获取元素节点
        var obox = document.querySelector('.box');
        console.log(obox);
        var oem = document.querySelector('.box em');
        console.log(oem);
        var ospan = document.querySelector('.box span');
        console.log(ospan);
        var onav = document.querySelector('.box nav');
        console.log(onav);


        console.log('-----------------华丽的分割线--------------');


        // 创建元素节点
            // document.createElement()
        var oh3 = document.createElement('h3')
        console.log(oh3);  // 
        oh3.innerHTML = '这是一个h3标签'

        var ostrong = document.createElement('strong');
        console.log(ostrong);  // 
        ostrong.innerHTML = '这是一个strong标签'

        var ofooter = document.createElement('footer');
        console.log(ofooter);  // 
        ofooter.innerHTML = '这是一个footer标签'


        console.log('-----------------华丽的分割线--------------');


        // 添加(插入) 元素节点
            // ele.appendChild(node)
                // 将 node 插入到 ele 内部的最后
            obox.appendChild(oh3);


            // ele.insertBefore(newnode,node);
                //  将 newnode 插入到 ele 内部的node 之前
            obox.insertBefore(ostrong,oem);

        
        console.log('-----------------华丽的分割线--------------');


        // 删除 元素节点
            // ele.removeChild(node)
                // 删除 ele 中的 node
        obox.removeChild(ospan);


        console.log('-----------------华丽的分割线--------------');

        
        // 克隆 元素节点
            // ele.cloneNode();
                // 如果 不传参 只会 克隆 这个元素自身
                // 如果 传参为true 就会 将这个元素及其内部的所有全部 克隆
        let o1 = obox.cloneNode();
        console.log(o1);

        let o2 = obox.cloneNode(true);
        console.log(o2);


        document.body.appendChild(o1);
        document.body.appendChild(o2);


        // 替换 元素节点
            // ele.replaceChild(newnode,node);
                // 将 ele 中的 node 替换成 newnode
        obox.replaceChild(ofooter,onav);

        



        // var s = document.createTextNode('1123haha');
        // console.log(s);
    </script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值