day08

1.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=1200, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <ul>
        <li>Java</li>
        <li>JavaScript</li>
        <li>Python</li>
    </ul>
    <script>
        const oList = document.querySelector('ul')
        const oE = document.createElement('li')
        //oE.append('Swift')
        oE.textContent = 'swift'
       // oList.append(oE)  // 插入成为最后一个节点
       // oList.prepend(oE)  // 插入成为第一个节点
       // oList.lastElementChild.before(oE) // 最后一个子节点的前面一个
       //oList.firstElementChild.after(oE)  /// 插在第一个子节点之后
       // oList.firstElementChild.nextElementSibling.remove()
       oList.lastElementChild.replaceWith(oE)
    </script>
</body>
</html>

2.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=1200, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button>click</button>

    <script>
        const oButton = document.querySelector('button')
        const callback = function () {
            console.log('Hello, Event')
        }

        oButton.addEventListener('click', callback)
        oButton.addEventListener('click', ()=> {
            console.log('hello, event1')
        })

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

3.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=1200, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button id='btnTest1'>click me</button>
    <button id="btnTest2" onclick='console.log("test")'>click me 2</button>
    <script>
        const myButton = document.querySelector('#btnTest1')
       /*
       
        myButton.onclick = function () {
            console.log('onclick')
        }
        // 通过这种方式只能设置一个事件处理程序。
        myButton.onclick = function() {
            console.log('hello')
        }
        //myButton.onclick = null   // 去掉绑定
        */
        
        const callback = () => {
            console.log('callback1')
        }
        const callback2 = () => {
            console.log('callback2')
        }
        const callback3 = () => {
            console.log('callback3')
        }

        myButton.addEventListener('click', callback)
        myButton.addEventListener('click', callback2)
        myButton.addEventListener('click', callback3)


        //myButton.removeEventListener('click',callback, false)
    </script>
</body>
</html>

4.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=1200, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box {
            width: 200px;
            height: 200px;
            border: 1px solid red;
            background-color:gray;
        }
    </style>
</head>
<body>
    <div id="box">box</div>
    <script>
        const oBox = document.querySelector('#box')

        window.addEventListener('click',() => {
            console.log('==> window 捕获阶段')
        },true)
        document.addEventListener('click',() => {
            console.log('==> document 捕获阶段')
        }, true)
        document.documentElement.addEventListener('click',() => {
            console.log('==> html 捕获阶段')
        }, true)
        document.body.addEventListener('click',(event) => {
            console.log('==> body 捕获阶段')
            event.stopPropagation()
        }, true)
        oBox.addEventListener('click',() => {
            console.log('==> box 捕获阶段')
        }, true)

        window.addEventListener('click',() => {
            console.log('==> window 冒泡阶段')
        },false)
        document.addEventListener('click',() => {
            console.log('==> document 冒泡阶段')
        }, false)
        document.documentElement.addEventListener('click',() => {
            console.log('==> html 冒泡阶段')
        }, false)
        document.body.addEventListener('click',() => {
            console.log('==> body 冒泡阶段')
        }, false)
        oBox.addEventListener('click',() => {
            console.log('==> box 冒泡阶段')
        }, false)
    </script>
</body>
</html>

5.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=1200, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div>my div</div>
    <script>
        const myDiv = document.querySelector('div')

        myDiv.addEventListener('click',(event) => {
            console.log(`${event.type}`)
        })

        const helloEvent = new Event('hello',{bubbles:true,cancelable:false})

        myDiv.addEventListener('hello',() => {
            myDiv.style.backgroundColor = "red"
            console.log(event.isTrusted)
        })

        myDiv.dispatchEvent(helloEvent)
    </script>
</body>
</html>

6.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=1200, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button>click me</button>
    <script>
        const oButton = document.querySelector('button')

        oButton.addEventListener('click', (event) => {
            for (let prop in event) {
                console.log(prop)
            }
        })
    </script>
</body>
</html>

7.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=1200, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box1 {
            width: 200px;
            height: 200px;
            background: gray;
            padding: 20px;
            margin: 20px;
        }

        #box2 {
            width: 100px;
            height: 100px;
            background-color: green;
            padding: 5px;
        }
    </style>
</head>

<body>
    <div id="box1">
        <div id="box2">click me</div>
    </div>
    <script>
        const box = document.querySelector('#box2')
        box.addEventListener('click', (event) => {
            console.clear()
            console.log(`clientX=${event.clientX}, clientY=${event.clientY}`)
            console.log(`ScreenX=${event.screenX}, screenY=${event.screenY}`)
            console.log(`pageX=${event.pageX}, pageY=${event.pageY}`)
            console.log(`offsetX=${event.offsetX}, offsetY=${event.offsetY}`)
            console.log(`x=${event.x}, y=${event.y}`)
            console.log(`layerX=${event.layerX}, layerY=${event.layerY}`)
            console.log(`event.type=${event.type}`)
            console.log(`event.target=${event.target}`)
            console.log(`event.currentTarget=${event.currentTarget}`)
        }, true)

        const callback = function (event) {
            switch (event.type) {
                case "mouseover":
                    event.target.style.backgroundColor = 'red'
                    break;
                case "mouseout":
                    event.target.style.backgroundColor = "blue"
                    break;
            }
        }
        box.addEventListener("mouseover", callback)
        box.addEventListener('mouseout',callback)
    </script>
</body>

</html>

8.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=1200, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div>
        <p>这是一个段落</p>
    </div>
    <script>
        const oDiv = document.querySelector('div')
        const oPara = document.querySelector('p')
        // const oPhase = {
        //     1: '捕获阶段',
        //     2: '目标阶段',
        //     3: '冒泡阶段'
        // }

        // const callback = (event) => {
        //     console.log(`元素:${event.currentTarget.tagName},传播阶段:${oPhase[event.eventPhase]}`)
        // }
        const callback = (event) => {
            console.log(`元素:${event.currentTarget.tagName},传播阶段:${event.eventPhase}`)
        }
        oDiv.addEventListener('click',callback,true)
       // oPara.addEventListener('click',callback,true)
        oDiv.addEventListener('click',callback,false)
        oPara.addEventListener('click', callback, false)
    </script>
</body>
</html>

9.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=1200, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <a id='link' href="https://www.microsoft.com">Microsoft HomePage</a>
    <input type="text">
    <script>
        const oLink = document.querySelector('#link')

        oLink.addEventListener('click',(event) => {
            if (event.cancelable) {
                event.preventDefault()
            }
            console.log(event.defaultPrevented)
            console.log('hello, link')
        })


        const oText = document.querySelector('input')

        oText.addEventListener('keypress',(event) => {
            if (event.charCode < 97 || event.charCode > 122) {
                event.preventDefault()
            }
        })
    </script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值