会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132886个问题
WEB前端全系列/第二阶段:JavaScript编程模块/面向对象编程 422楼
WEB前端全系列/第二阶段:JavaScript编程模块/运算符_数据类型和流程循环语句 426楼

                                                         <Script>

                                                            let alt="";

                                                            for (let i=1;i<=5 ; i++) {

                                                                let blank="";

                                                                for (let j=1;j<=5-i; j++) {

                                                                    blank+=" ";

                                                                }

                                                                let stars="";

                                                                for (let m=1; m<=2*i-1; m++) {

                                                                    stars+="*";

                                                                }

                                                              alt+=blank+stars+"\n";

                                                            }

                                                            console.log(alt);

                                                        document.write(alt+"<br/>");

                                                        

                                                        </Script>

                                         页面输出的效果出了br和n不一样,为什么第一距离页面输出和控制台的不一样呢

WEB前端全系列/第二阶段:JavaScript编程模块/运算符_数据类型和流程循环语句 427楼
WEB前端全系列/第二阶段:JavaScript编程模块/DOM模型 429楼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>带有hover样式的导航栏</title>
</head>
<body>
        <script>
            // 创建ul并设置样式
            var ul=document.createElement('ul');
            ul.style.cssText='padding: 0; list-style: none;';

            // for循环创建li并设置属性,并设置hover样式
            var titleArr=['旅游','美食','军事','科技','数码'];
            for (var i=0; i<titleArr.length; i++) {
                var li=document.createElement('li');
                li.style.cssText='width: 80px; height: 40px; line-height: 40px;\
                background-color: skyblue; display:inline-block; text-align: center;';
                li.innerHTML=titleArr[i];
                ul.appendChild(li);
                li.onmouseenter=function () {
                    this.style.cssText='background-color: yellow;';
                }
                li.onmouseleave=function () {
                    this.style.cssText='background-color: skyblue;';
                }
            }
            document.body.appendChild(ul);
        </script>
</body>
</html>

鼠标不放上去时,是第一张图的效果,放上去之后就变成2、3图的效果

QQ截图20201211095739.png

QQ截图20201211095751.png

QQ截图20201211095725.png


WEB前端全系列/第二阶段:JavaScript编程模块/浏览器模型(BOM) 430楼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>解决浏览器事件绑定兼容性问题</title>
    <style>
        .div1 {
            width: 200px;
            height: 200px;
            background-color: paleturquoise;
        }
        .div2 {
            margin: 25px auto;
            width: 100px;
            height: 100px;
            background-color: orange;
        }
    </style>
</head>
<body>
    <div class="div1">div1
        <div class="div2">div2</div>
    </div>
<script>
    var div1=document.querySelector('.div1');
    var div2=document.querySelector('.div2');

    function func () {
        console.log('这是div');
    }
    // function func2 () {
    //     console.log('IE里的div');
    // }

    var cleanBug={
        add:function (ele,type,func) {
                if (ele.addEventListener) {
                    ele.addEventListener(type,func);
                }else {
                    ele.attachEvent('on'+type,func);
                }
            },
        remove:function (ele,type,func) {
                    if (ele.removeEventListener) {
                        ele.removeEventListener(type,func);
                    }else {
                        ele.detachEvent('on'+type,func);
                    }
                }    
            }

    cleanBug.add(div1,'click',func);
    // (div2,'click',func);
</script>    
</body>
</html>

老师,我问两个问题:

1、如果两个div是嵌套关系,我给外面的div敲击事件,里面的div点击它也会产生敲击事件?

2、我想给div1和div2各一个敲击事件且让他们打印的值不一样,代码该怎么修改?

WEB前端全系列/第二阶段:JavaScript编程模块/浏览器模型(BOM) 431楼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>html事件</title>
    <style>
        .div1 {
            width: 200px;
            height: 200px;
            background-color: paleturquoise;
        }
        .div2 {
            margin: 25px auto;
            width: 100px;
            height: 100px;
            background-color: orange;
        }
    </style>
</head>
<body>
    <div class="div1" onclick="text1()">div1
        <div class="div2" onclick="text3(); text2()">div2</div>
    </div>
<script>
    var div1=document.querySelector('.div1');
    var div2=document.querySelector('.div2');
    function text1() {
        console.log('这是第一个函数');
    }
    function text2() {
        div2.setAttribute('onclick',null);
        console.log('这是第二个函数');
    }
    function text3() {
        div2.setAttribute('onclick',null);
        console.log('这是第三个函数');
    }
    // div2.setAttribute('onclick',null);
</script>
</body>
</html>

问题一:我在函数里面写了事件移除的代码,为啥点击div2还是执行了代码?

问题二,既然我们要删除事件了,为啥代码不写在外面呢?就像最后一行注释的代码。

QQ截图20201208222729.png


WEB前端全系列/第二阶段:JavaScript编程模块/浏览器模型(BOM) 432楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637