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

<!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>
        .div1{
            width: 200px;
            height: 200px;
            background-color: red;
        }
        .div2{
            width:60px ;
            height: 50px;
            background-color: orange;
        }
    </style>
</head>
<body>
    
    <div class="div1" onclick="test1()">
        <div class="div2"  onclick="test2();test3()"></div>
    </div>
    <script>
        var i=0;
        var div1=document.querySelector('.div1');
        /*html事件采用冒泡机制 子节点出发,传递给父节点
             绑定多个函数用分号隔开
              根据绑定函数的先后顺序,触发事件顺序    */
             
        
       function test1() {
        i++;
        alert('div1');
      
        console.log(i);
       }
       if (i>=3) {
           div1.setAttribute('onclick',null);
           
           
       } else {
           
       }
        function test2() {alert('div2');
           
        }
        function test3() {
            
           
        }

    </script>
</body>
</html>

老师,怎么才能点第三次div1的时候不会触发事件

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<ul>
    <li class="1">11</li>1111
    <li class="2"></li>
</ul>
<div id="div1"></div>
<script>
    var li=document.createElement('li');
    var li_text=document.createTextNode('1qianmian');
    li.appendChild(li_text);                              
     var ul=document.querySelector('ul');
    ul.insertBefore(li,ul.firstChild);                           
    /* 语法 noded.insertBefore(node1,node2)  
  node1是插入进去的节点 node2是node1插入在node2子节点前面(插入位置) 
    返回值 是返回插入的节点     */
var li_list=document.querySelectorAll('li'); /*node.removeChild(node1)    node为node1的父节点,node1为需要删除的节点*/
var div=document.querySelector('div');
ul.removeChild(li_list[0]);
console.log(li_list);
var but=document.createElement('button');
but.innerHTML='替换';
document.body.appendChild(but);
but.onclick=function(){
  var span=document.createTextNode('tihuan')
  
    ul.replaceChild(span,ul.childNodes[1]);
    
}

/*node.contains(node1) node是否包含node1 node为node1 父节点 返回布尔值   */
var buer=ul.contains(ul.childNodes[1]);
console.log(buer);
    
</script>
</body>
</html>

老师为啥var buer=ul.contains(ul.childNodes[1]) ul包含li  但是代码写成var buer=ul.contains(li)就是false

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body><span>11
        <div>
            22
        </div>
        <div>3</div>
    </span>

    <script>
        /*var span=document.querySelector('span');
console.log(span.nodeName); 通用属性节点.nodeName  node.Type
                                 节点.ownerDocument 返回当前节点所在顶层文档对象 Document .ownerDocument返回null
                                 节点.nextSibling属性返回此节点的下一个同级节点(回车也算一个节点,文本节点)没有返回null
                                  节点.previousSibling  属性返回此节点的上一个同级节点(回车也算一个节点,文本节点)没有返回null
                                  节点.parentNode 返回当前节点的父节点 ,没有返回null(例如document)
                                  节点.parentElement 返回当前节点的父元素节点 ,没有或者父节点不是元素节点返回null(例如document)
                                                                
console.log(span.nodeType);*/  

/*var span=document.querySelector('span');   节点.textContent 返回当前节点及当前节点的后代节点的文本内容
var text_Content=span.textContent;              节点.nodeValue  一般用书文本节点 返回当前节点的值
console.log(text_Content);*/

var span=document.querySelector('span');
console.log(span.childNodes);
console.log(document.body.lastChild)
console.log(document.body.firstChild)

    </script>
    aa
</body>
</html>

image.pnglk老师为啥body最后一个子节点不是aa吗 为啥是scrpit?

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>window对象的name属性</title>
</head>
<body>
    <button>跨域传输</button>
    <script>
/*         var name=123;
        console.log(name);
        console.log(typeof name);
        var age=123;
        console.log(age);
        console.log(typeof age); */

/*         window.name
        描述:是页面在切换之后,甚至域名更改之后会储存信息的容器
        说明:借助window.name可以实现页面之间数据的传递,称为跨域传输。 */
        //console.log(num);
        //如果想要获取一个页面的信息,那么必须先加载
        var but=document.querySelector('button');
        but.onclick=function(){
            var iframe=document.createElement('iframe');
            iframe.src='page.html';//加载保存了信息的页面
            iframe.style.display='none';
            document.body.appendChild(iframe);
            //当iframe加载完成,意味着window.name的内容已经被赋予完成
            iframe.onload=function(eve){
                var iframeWindowName=eve.target.contentWindow.name;
                //console.log(iframeWindowName);
                eval(iframeWindowName);
                console.log(num);

            }
        }
    </script>
</body>
</html>

image.png老师为什么这一行在vscode里运行会报错误呢?在WebStorm 就可以运行!!

image.png

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

课程分类

百战程序员微信公众号

百战程序员微信小程序

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