会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133059个问题

<!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) 436楼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        /* ul {
            list-style: none;
        }
        li {
            display: inline-block; width: 100px; margin: 10px; text-align: center; background-color: skyblue; height: 30px; line-height: 30px;
        }
        a {
            text-decoration: none;
            color: orangered;
        }*/
        li:hover {
            background-color: palevioletred;
        }
        li:hover a {
            color: #ffffff;
        } 
    </style>
</head>
<body>
    <!-- <ul>
        <li><a href="#">丰田</a></li>
        <li><a href="#">本田</a></li>
        <li><a href="#">雷克萨斯</a></li>
        <li><a href="#">马自达</a></li>
    </ul> -->

    <script>
        var Arr=['丰田','本田','雷克萨斯','马自达','尼桑'];
        var ul=document.createElement('ul');
        var ul_style=document.createAttribute('style');
        ul_style.value='list-style: none;';
        ul.setAttributeNode(ul_style);
        for (var i=0; i<Arr.length; i++) {
            var li=document.createElement('li');
            var li_style=document.createAttribute('style');
            li_style.value='display: inline-block; width: 100px; margin: 10px; \
            text-align: center; background-color: skyblue; height: 30px; line-height: 30px;';
            li.setAttributeNode(li_style);
            var a=document.createElement('a');
            a.innerHTML=Arr[i];
            // var a_text=document.createTextNode(Arr[i]);
            var a_style=document.createAttribute('style');
            a_style.value='text-decoration: none; \
            color: orangered;';
            // a.appendChild(a_text);
            a.setAttributeNode(a_style);
            li.appendChild(a);
            ul.appendChild(li);
        }
        document.body.appendChild(ul);
    </script>
</body>
</html>

老师,我写出来之后,网页上确实显示了,但是hover失效了,而且a标签也没有小手了。

QQ截图20201205142438.png


WEB前端全系列/第二阶段:JavaScript编程模块/DOM模型 440楼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>自定义右键菜单案例</title>
    <style>
        ul{width: 200px;background: #f8f8f8;border: 1px solid #cccc;list-style: none;padding: 0;display: none;position: absolute;}
        ul li{height: 30px;line-height: 30px;font-size: 14px;padding-left: 10px;cursor: pointer;}
        ul li:hover{background-color: teal;}
    </style>
</head>
<body>
    <textarea cols="30" rows="10"></textarea>
    <ul>
        <li>你是谁?</li>
        <li>你确定要离开本页面吗?</li>
        <li>选中的内容跳转到百度搜索</li>
        <li>输入的内容跳转到百度搜索</li>
    </ul>
    <script>
        //系统右键菜单禁用事件【contextmenu】
        document.oncontextmenu = function (eve) {
            return false;   //return false表示事件禁用
        };
        //鼠标右键点击任意位置显示右键菜单
        var ul = document.querySelector('ul');
        document.onmouseup = function(eve){
            //eve.button可以判断用的是鼠标的哪一个按钮
            //左键是0,滚轮是1,右键是2
            //console.log(eve.button);
            if(eve.button == 2){
                //设置鼠标点击的位置
                //记住记住记住,必须给‘px’
                ul.style.left = eve.clientX + 'px';
                ul.style.top = eve.clientY + 'px';
                ul.style.display = 'block';
            }else{
                ul.style.display = 'none';
            }
        }
        //采用事件委托,点击每一个选项时触发的事件
        ul.onclick = function(eve){
            if(eve.target.innerHTML == '你是谁?'){
                alert('我是我')
            }else if(eve.target.innerHTML == '你确定要离开本页面吗?'){
                //判断点击的是确定还是取消
                if(confirm(eve.target.innerHTML == '你确定要离开本页面吗?')){
                    window.close();
                }
            }else if(eve.target.innerHTML == '选中的内容跳转到百度搜索'){
                var result = document.getSelection().toString();//getSelection()获取选中的内容,然后转成字符串就可以搜索了
                window.open('http://www.baidu.com/s?wd=' + result);
            }else{
                var result1 = prompt('搜索什么');
                window.open('http://wwww.baidu.com/s?wd=' + result1);
            }
        }
    </script>
</body>
</html>

老师,我的第三个只能跳到百度,不能直接选中搜索

WEB前端全系列/第二阶段:JavaScript编程模块/面向对象编程 445楼
WEB前端全系列/第二阶段:JavaScript编程模块/面向对象编程 447楼

QQ截图20201130165548.png

如果按第一种写法,意思就是把所有的关键字都默认当成字符串,用方括号写就默认把关键字还当作关键字?

WEB前端全系列/第二阶段:JavaScript编程模块/函数与对象 449楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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