js例题之鼠标事件、固定导航条、tab切换、自定义属性、节点、留言、表格

例题总结

1.onmouseover和onmouseout的区别

常见鼠标事件:

onclick:鼠标点击左键触发

onmouseover:鼠标经过触发

onmouseout:鼠标离开触发

onfocus:获得鼠标焦点触发

onblur:失去鼠标焦点触发

onmousemove:鼠标移动触发

onmousedown:鼠标按下触发

onmouseup:鼠标弹起触发

 <style>
        img{
            width: 100px;
        }
    </style>
</head>
<body>
    <img src="./images/1.jfif" alt="">
    <script>
        document.querySelector('img').οnmοuseοver=function(){
            this.src='./images/2.jpg'
        }
        document.querySelector('img').οnmοuseοut=function(){
            this.src='./images/1.jfif'
        }
    </script>

2.京东固定导航条

<!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;
        }

        body {
            background-color: #eee;
        }

        .nav {
            position: fixed;
            right: 10px;
            top: 50px;
            width: 60px;
            background-color: #fff;
        }

        .nav ul {
            list-style-type: none;
        }

        .nav li {
            padding: 10px 10px 5px 10px
        }

        .nav a {
            display: block;
            border-bottom: 1px solid #eee;
            padding-bottom: 5px;
            color: #333;
            text-decoration: none;
            font-size: 14px;
        }

        /* .active {
            background-color: #e1251b;
            color: #fff;
        } */
    </style>
</head>

<body>
    <div class="nav">
        <ul>
            <li><a href="#">京东秒杀</a></li>
            <li><a href="#">特色优选</a></li>
            <li><a href="#">频道广场</a></li>
            <li><a href="#">为你推荐</a></li>
        </ul>
    </div>
    <script>
        var lis = document.querySelectorAll('li')
        /*在循环当中,只是为每一个li标签注册onmouseover事件,但是事件中的代码
        只有当事件被处罚时才会执行*/
        for (var i = 0; i < lis.length; i++) {
            // 当鼠标进入元素时候触发这个事件
            lis[i].onmouseover = function () {
                this.style.backgroundColor = '#e1251b'
                // console.log(this.children[0]);
                this.children[0].style.color='#fff'
            }
            // 当鼠标从此元素上离开后触发
            lis[i].οnmοuseοut=function(){
                this.style.backgroundColor = '#fff'
                // console.log(this.children[0]);
                this.children[0].style.color='#333'
            }
        }

    </script>
</body>

</html>

3.tab切换

<!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;
        }

        body {
            background-color: #eee;
        }

        .nav {
            position: relative;
            width: 500px;
            left: 400px;
            height: 300px;
            background-color: #fff;
        }

        .nav ul {
            float: right;
            list-style-type: none;
            height: 30px;
            /* border: 1px solid red; */
            overflow: hidden;
        }

        .nav li {
            float: left;
            margin: 0 13px;
            font-size: 14px;
            color: #333;
            padding-bottom: 2px;
            cursor: pointer;
        }

        .active {
            color: #e1251b !important;
            border-bottom: 2px solid #e1251b;
        }

        .content {
            position: absolute;
            top: 30px;
            width: 500px;
            height: 270px;
            text-align: center;
            line-height: 270px;
            /* border: 1px solid black; */
        }
    </style>
</head>

<body>
    <div class="nav">
        <ul>
            <li class="active">精选</li>
            <li>美食</li>
            <li>游戏</li>
            <li>家居</li>
            <li>宇宙飞船</li>
        </ul>
        <div class="content">
            
        </div>
    </div>
    <script>
        // 获取content
        var content = document.querySelector('.content')
        content.innerHTML='精选'
        // 获取所有li标签
        var lis = document.querySelectorAll('li')
        for (var i = 0; i < lis.length; i++) {
            lis[i].onmouseover = function () {
                // 将所有li标签的class 属性的值设置为空字符串即可
                reset()
                // 将当前悬浮的li标签的文字颜色和下边框变为红色
                this.className = 'active'
                console.log(this.innerHTML);
                var txt = this.innerHTML
                if (txt == '精选') {
                    content.innerHTML = '精选'
                } else if (txt == '美食') {
                    content.innerHTML = '美食城'
                } else if (txt == '游戏') {
                    content.innerHTML = '游戏'
                } else if (txt == '家居') {
                    content.innerHTML = '家居'
                } else if (txt == '宇宙飞船') {
                    content.innerHTML = '宇宙飞船'
                }
            }
        }
        function reset() {
            for (var i = 0; i < lis.length; i++) {
                lis[i].className = ''
            }
        }
    </script>
</body>

</html>

4.自定义属性

自定义属性目的:是为了保存并使用数据。有些数据可以保存到页面中而不用保存到数据库中。
自定义属性获取是通过
getAttribute(‘属性’)
获取

<body>
    <p id="1" data-zhx="18" data-index="20">美女</p>
    <button id="btn_get">获得属性</button>
    <button id="btn_set">设置属性</button>
    <script>
        var p=document.querySelector('p')
        document.querySelector('#btn_get').οnclick=function(){
            // 仍然可以使用getAttribuet方法获取带data-前缀的属性
            console.log(p.getAttribute('data-index'))
            // 也可以用dataset.属性名称的方法获取
            console.log(p.dataset.index)
            console.log(p.dataset.zhx)
        }
    </script>

5.父节点

parentNode属性可返回某节点的父节点,注意是最近的一个父节点

如果指定的节点没有父节点则返回null

<body>
    <ul>
        <li id="li1"> 帅哥</li>
        <li>美女</li>
    </ul>
    <script>
        var lis=document.querySelector('#li1');
        console.log(li1.parentNode);
        // 获取父节点   <ul>...</ul>
    </script>
</body>
</html>

6.子节点

<body>
    <ul>
        <li>张</li>
        <li>慧宣</li>
        <li>宣宣宣宣</li>
        <li>宣宣</li>
    </ul>
    <script>
        var ul = document.querySelector('ul')
        // childNodes将空白也当做子节点
        console.log(ul.childNodes); //(标准)
        
        //虽然是非标准的,但是各个浏览器都支持
        console.log(ul.children);

        // 根据索引获取第一个子节点
        ul.children[0],style.color='red'
        // 使用属性获取第一个子节点
        ul.firstElementChild,style.color='red'  //谷歌支持这个
        ul.firstChild.style.color='red'  //IE8才能获取
        /* 兼容性问题
         
         */
    </script>
</body>
</html>

这两个方法有兼容性问题,解决方案:

(1) 用索引方式
(2)var first=ul.firstElementChild || ul.firstChild

7.创建,添加,删除节点

创建节点:document.creatElement(‘tagName’)

添加节点:node.appendChild(child) node.insertBefore(child,指定元素)

删除节点:node.removeChild(child)

node.remove()

<!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>熊大</li>
        <li id="li2">熊二</li>
        <li id="li3">熊三</li>
        <li>熊四</li>
    </ul>
    <button id="btn_add">添加</button>
    <button id="btn_del">删除</button>
    <script>
        var button = document.querySelector('#btn_del')
        var li3 = document.querySelector('#li3')
        var btn_add = document.querySelector('#btn_add')
        button.onclick = function () {
            // li3.remove()
            document.querySelector('li:nth-child(3)').remove()
        }
        // 添加
        btn_add.onclick = function () {
            var liNode = document.createElement('li')
            liNode.id='li5'
            liNode.innerHTML = '熊五'
            li3.insertAdjacentElement('beforebegin', liNode)
        }
    </script>
</body>

</html>

注意

  • beforebegin:在当前元素节点的前面。
  • afterbegin:在当前元素节点的里面,插在它的第一个子元素之前。
  • beforeend:在当前元素节点的里面,插在它的最后一个子元素之后。
  • afterend:在当前元素节点的后面。

8.留言

<!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;
        }
        body{
            background-color: #eee;
        }
        ul{
            list-style-type: none;
        }
        .box{
            width: 800px;
            height: 400px;
            background-color: #fff;
            margin: 10px auto;
            padding: 20px;
        }
        
        .message{
            width: 600px;
            height: 34px;
            border: 1px solid #eee;
            border-radius: 10px;
            outline: none;
            font-size: 18px;
            padding-left: 10px;
        }
        .btn_publish{
            width: 100px;
            height: 34px;
            background-color: orange;
            border: none;
            color: #fff;
            border-radius: 10px;
            outline: none;
        }
        .message_list{
            margin-top: 20px;

        }
        .message_list li{
            margin-bottom: 30px;
            /* border: 1px solid black; */
            overflow: hidden;
            width: 700px;
        }
        .msg{
            float: left;
        }
        .btn_delete{
            float: right;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="publish_box">
            <input type="text" class="message" placeholder="请撰写留言">
            <button class="btn_publish">提交评论</button>
        </div>
        <ul class="message_list">
            <!-- <li>
                <span class="msg">今天天气不错</span>
                <a href="#" class="btn_delete">删除</a>
            </li>           -->
        </ul>
    </div>
    <script>
        var ul=document.querySelector('.message_list')
        var input=document.querySelector('.message')
        
       
        document.querySelector('.btn_publish').οnclick=function(){
            var txt=input.value
            if(txt==''){
                alert('留言不能为空')
                // 后面的代码不会执行
                return false
            }           
            var liNode=document.createElement('li')
            
            liNode.innerHTML=`
                <span class="msg">${txt}</span>
                <a href="#" class="btn_delete">删除</a>`                
                
            ul.insertAdjacentElement('afterbegin',liNode)
            // 为新创建的li节点注册单击事件
            liNode.children[1].οnclick=function(){
                this.parentNode.remove()
            }
            // 清空文本框内容
            input.value=''
            // 让文本框获取焦点
            input.focus()
        }
    </script>
</body>

</html>

9.表格(添加和删除)

<!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;
        }

        table {
            width: 800px;
            border: 1px solid #eee;
            border-collapse: collapse;
            margin: 10px auto;
            text-align: center;
        }

        th,
        td {
            border: 1px solid #eee;
            padding: 15px;
        }

        .box {
            width: 800px;
            margin: 10px auto;
        }
    </style>
</head>

<body>
    <div class="box">
        <label for="">编号</label>
        <input type="text" id="number">
        <label for="">姓名</label>
        <input type="text" id="nearname">
        <button id="btn_add">添加</button>
    </div>
    <table>
        <thead>
            <tr>
                <th>编号</th>
                <th>姓名</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>001</td>
                <td>詹姆斯邦德</td>
                <td>
                    <a href="#" class="delete">删除</a>
                </td>
            </tr>

        </tbody>
    </table>
    <script>
        var tbody = document.querySelector('tbody')
        document.querySelector('#btn_add').onclick = function () {
            var trNode = document.createElement('tr')
            // 获取编号
            var number = document.querySelector('#number').value
            // 获取姓名
            var nearname = document.querySelector('#nearname').value
            trNode.innerHTML = `
                <td>${number}</td>
                <td>${nearname}</td>
                <td>
                    <a href="#" class="delete">删除</a>
                </td>
            `
            tbody.insertAdjacentElement('beforeend', trNode)
            // 为新创建的行注册单击事件
            var btn_delete = trNode.querySelector('.delete')
            btn_delete.onclick = function () {
                this.parentNode.parentNode.remove()
            }
            // 为新创建的td注册双击事件
            var td_arr = trNode.querySelectorAll('td')
            for (var i = 0; i < td_arr.length; i++) {
                td_arr[i].ondblclick = function () {
                    console.log(this);
                    // 创建文本框
                    var input = document.createElement('input')
                    input.type = 'text'
                    input.value = this.innerHTML
                    this.innerHTML = ''
                    this.insertAdjacentElement('afterbegin', input)
                    input.focus()
                    // 为文本框注册失去焦点事件
                    input.onblur = function () {
                        var txt = this.value
                        this.parentNode.innerHTML = txt
                    }
                    console.log(input);
                }
            }
        }
    </script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值