day07

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>
    <style>
        
    </style>
</head>
<body>
    <button onclick='console.log("hello");'></button>
    <script>
        console.log('hello')
    </script>
    <script src="1.js"></script>
</body>
</html>

1.js

console.log('hello')

2.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=1200, initial-scale=1.0">
    <title>访问非常规元素</title>
</head>
<body>
    <div id='myDiv'>test</div>
    <script>
        console.log(document.doctype)
        console.log(typeof document.doctype)
        console.log(document.documentElement)
        console.log(document.head)
        console.log(document.title)
        console.log(document.body)
    </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>
    <div id="myDiv" name="div1" class='test1'>
        <p id="myPara">test</p>
    </div>
    <script>
        const oDiv = document.getElementById('myDiv')
        const oDiv1 = document.getElementsByName('div1')
        const oDiv2 = document.getElementsByTagName('div')
        const oDiv3 = document.getElementsByClassName('test1')
        const oDiv4 = document.querySelector('#myDiv')
        const oDiv5 = document.querySelectorAll('#myDiv')
        console.log(oDiv,oDiv1,oDiv2,oDiv3)
        console.log(oDiv4,oDiv5)
        const oPara = oDiv.querySelector('#myPara')
    </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>
        .p1 {
            border: 1px solid red;
        }
        .p2 {
            background-color:gray;
        }
        .p3 {
            visibility:hidden;
        }
    </style>
</head>

<body>
    <!-- 
    // 原生属性
    <div id="myDiv" class="test" style="border:1px solid red;"></div>
    <img src="" alt="">
    <label for="userName" >姓名:</label><input type="text" name="userName" id="">
    <script>
        const oDiv = document.querySelector('div')
        console.log(oDiv.id)
        console.log(oDiv.className)
        const oLabel = document.querySelector('label')
        console.log(oLabel.htmlFor)
        console.log(oDiv.style)
    </script> -->
    <!--ClassName-->
    <h1 class="p1 p2">this is a heading</h1>
    <button id='myButton'>Click</button>

    <script>
        const heading = document.querySelector('h1')
        const myButton = document.querySelector("#myButton")
        //console.log(heading.className)
        //heading.className += ' p3'
        //console.log(heading.className)
        //classList
        console.log(heading.classList)
        //heading.classList.add('p3')
        heading.classList.remove('p2')
        myButton.addEventListener('click',() =>{
            heading.classList.toggle('p3')
        })
    </script>

    <!--自定义属性-->
    <div id="myDiv1" ddd="user" eee="name">test</div>
    <script>
        const oDiv1 = document.querySelector('#myDiv1')
        let ddd = oDiv1.getAttribute('ddd')
        let id = oDiv1.getAttribute('id')
        console.log(ddd) //user
        console.log(id) //myDiv1
        oDiv1.setAttribute('eee', 'xiaojichao')
        let eee = oDiv1.getAttribute('eee')
        console.log(eee)
    </script>

    <!--HTML5 自定义数据属性-->
    <div id="myDiv2" data-user-name="Tianguiping" data-title="teacher">Test2</div>
    <script>
        const myDiv2 = document.querySelector('#myDiv2')
        console.log(myDiv2.dataset)
        console.log(myDiv2.dataset.userName)
        myDiv2.dataset.title = 'student'
        console.log(myDiv2.dataset.title)
    </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>
    <h1>this is a heading</h1>
    <p>this is <i>a little</i> sample</p>
    <script>
        const myPara = document.querySelector('p')

        // beforebegin、afterbegin、beforeend、 afterend
        myPara.insertAdjacentHTML('beforebegin','<i>test</i>')
        myPara.insertAdjacentHTML('beforeend','<i>test2</i>')
        myPara.insertAdjacentText('afterbegin','testsdasdfasdfa;sdjf')
        console.log(myPara.innerHTML)
        console.log(myPara.outerHTML)

        ///= 只访问里面的文本
        console.log(myPara.textContent)
        console.log(document.documentElement.textContent)
        console.log(myPara.innerText)
    </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>
    <ul>
        <li>Java</li>
        <li>JavaScript</li>
        <li>Python</li>
        <li>Swift</li>
    </ul>
    <script>
        const oList = document.querySelector('ul')
        console.log(oList.parentNode)
        console.log(oList.parentElement)
        // 得到 oList 元素的子节点
        console.log(oList.childNodes)
        console.log(oList.children)
        //访问第一个元素
        console.log(oList.firstChild)
        console.log(oList.firstElementChild)
        //访问最后一个元素
        console.log(oList.lastElementChild)
        // 访问相邻的元素
        console.log(oList.firstElementChild.nextElementSibling)
        console.log(oList.lastElementChild.previousElementSibling)
        // 元素节点的数目
        console.log(oList.childNodes.length)
        console.log(oList.childElementCount)
    </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>
        .p1 {
            visibility: hidden;
        }
    </style>
</head>
<body>
    <ul>
        <li>Java</li>
        <li>JavaScript</li>
    </ul>
    <input type="text" id="txtItem" />
    <button id="btnAdd">+</button>
    <button id="btnDel">-</button>
    <script>
        const oList = document.querySelector('ul')

        document.querySelector('#btnAdd').addEventListener('click',() => {
            let strItem = document.querySelector("#txtItem").value
            let oE = document.createElement('li')
            oE.textContent = strItem
            oList.appendChild(oE)
            document.querySelector("#txtItem").value = ''
            if (oList.childElementCount > 0) {
                document.querySelector('#btnDel').classList.remove('p1')
            }
        })

        document.querySelector('#btnDel').addEventListener('click',() => {
            oList.removeChild(oList.lastElementChild)
            if (oList.childElementCount == 0) {
                document.querySelector('#btnDel').classList.toggle('p1')
            }
        })
    </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>
    <style>
        .p1 {
            visibility: hidden;
        }
    </style>
</head>
<body>
    <ul>
        <li>Java</li>
        <li>JavaScript</li>
    </ul>
    <input type="text" id="txtItem" />
    <button id="btnAdd">+</button>
    <button id="btnDel">-</button>
    <script>
        const oList = document.querySelector('ul')

        document.querySelector('#btnAdd').addEventListener('click',() => {
            let strItem = document.querySelector("#txtItem").value
            let oE = document.createElement('li')
            oE.textContent = strItem
            oList.appendChild(oE)
            document.querySelector("#txtItem").value = ''
            if (oList.childElementCount > 0) {
                document.querySelector('#btnDel').classList.remove('p1')
            }
        })

        document.querySelector('#btnDel').addEventListener('click',() => {
            oList.removeChild(oList.lastElementChild)
            if (oList.childElementCount == 0) {
                document.querySelector('#btnDel').classList.toggle('p1')
            }
        })
    </script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值