jq兄弟选择器及节点

本文介绍了jQuery中的兄弟选择器,包括prevAll(), nextAll(), 和siblings(),并提供了相关示例。同时,讲解了节点操作,如append(), appendTo(), after(), before(), empty()和remove()的方法用法。" 48803923,5036415,UML建模在机房收费系统中的应用与体会,"['UML建模', '面向对象设计', '软件设计', '机房管理', '系统集成']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        

什么是兄弟元素?

<ul id="list">
    <li class="li-1">web前端</li>
    <li class="li-2">web前端</li>
    <li class="li-3">web前端</li>
    <li class="li-4">web前端</li>
    <li class="li-5">web前端</li>
</ul>
 

1、$(selector).prevAll( selector )

功能描述:获得集合中每个匹配元素的所有前面的兄弟元素,选择性筛选的选择器。 

$(".li-3").prevAll("li");   // ['li-1','li-2']

2、$(selector).nextAll( selector )

功能描述:获得每个匹配元素集合中所有下面的同辈元素,选择性筛选的选择器。 

$(".li-3").nextAll("li");   // ['li-4','li-5']

3、$(selector).siblings( selector )

功能描述:获得匹配元素集合中每个元素的兄弟元素,可以提供一个可选的选择器。

$(".li-3").siblings("li");  // ['li-1','li-2','li-4','li-5']
 

案例:

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        li {
            list-style: none;
        }
        
        body {
            background-color: black;
        }
        
        div {
            width: 700px;
            border: 1px solid white;
            margin: 0 auto;
            padding-right: 50px;
        }
        
        ul {
            width: 680px;
            height: 100%;
            display: flex;
            flex-wrap: wrap;
        }
        
        li {
            padding: 10px;
        }
    </style>
    <script src="../jq/jquery.min.js"></script>
</head>

<body>
    <div class="wrap">
        <ul>
            <li>
                <a href="#"><img src="../img/01.jpg" alt=""></a>
            </li>
            <li>
                <a href="#"><img src="../img/02.jpg" alt=""></a>
            </li>
            <li>
                <a href="#"><img src="../img/03.jpg" alt=""></a>
            </li>
            <li>
                <a href="#"><img src="../img/04.jpg" alt=""></a>
            </li>
            <li>
                <a href="#"><img src="../img/05.jpg" alt=""></a>
            </li>
            <li>
                <a href="#"><img src="../img/06.jpg" alt=""></a>
            </li>
        </ul>
    </div>
    <script>
        $(function() {
                //给li标签绑定鼠标移入事件
                $("ul li").mousemove(function() {
                    //如何获取到当前鼠标移入的li标签 this
                    $(this).css("opacity", 1).siblings().css("opacity", 0.5)
                })

                //给鼠标移入的li标签的opacity设置为1,将其他兄弟标签li设置为0.5
            })
            //鼠标移出
        $("ul li").mouseout(function() {
            $("ul li").css("opacity", 1)
        })
    </script>
</body>

</html>

 选项卡案例

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../jq/jquery.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        ul li {
            list-style: none;
        }
        
        #box {
            width: 300px;
            border: 1px solid #ccc;
            margin: 30px;
        }
        
        #box ul li {
            font-size: 20px;
            font-weight: bold;
            float: left;
            color: white;
            width: 100px;
            padding: 10px 0;
            text-align: center;
            background-color: rgb(255, 0, 0);
        }
        
        .active {
            background: rgb(165, 42, 42) !important;
        }
        
        .content div {
            font-size: 30px;
            font-weight: bold;
            display: none;
            height: 150px;
        }
        
        .show {
            display: block !important;
        }
    </style>
</head>

<body>
    <div id="box">
        <ul>
            <li class="active">Home</li>
            <li>About</li>
            <li>Login</li>
        </ul>
        <div class="content">
            <div class="show" style="background-color: green;">Home</div>
            <div style="background-color: greenyellow;">About</div>
            <div style="background-color: yellow;">Login</div>
        </div>
    </div>
    <script>
        $(function() {
            $("ul li").click(function() {
                $(this).addClass("active").siblings().removeClass("active")
                var index = $(this).index()
                $(".content div").eq(index).addClass("show").siblings().removeClass("show")
            })

        })
    </script>
</body>

</html>

 节点

1、append()方法的作用是向每一个匹配的元素内部追加内容。它的表达式是append(content|fn)。
2、append操作和对指定的元素执行appendChild方法从而把它们添加到文档中的情况相类似。
3、appendTo()方法的作用是把所有匹配的元素追加到另一个指定的元素集合中。它的表达式是appendTo(content)。

4、after()方法的作用是在每个匹配的元素之后插入内容。它的表达式是after(content|fn)。
5、before()方法的作用是在每个匹配的元素之前插入内容。表达式是before(content|fn)。

6、empty()方法的作用是删除匹配的元素集合中所有的子节点。
7、remove() 方法的作用是从DOM中删除所有匹配的元素。它的表达式是remove([expr])。

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        ul {
            list-style: none;
            line-height: 30px;
            width: 600px;
        }
        
        ul li:nth-child(even) {
            background-color: green;
        }
        
        ul li:nth-child(odd) {
            background-color: aqua;
        }
        
        span {
            color: red;
            font-size: 12px;
        }
    </style>
    <script src="../jq/jquery.min.js"></script>
</head>

<body>
    <button id="btn1">append添加</button>
    <button id="btn2">appendTo添加</button>
    <button id="btn3">prepend添加</button>
    <button id="btn4">prependTo添加</button>
    <button id="btn5">before添加</button>
    <button id="btn6">after添加</button>
    <button id="btn7">remove移除</button>
    <button id="btn8">empty移除</button>
    <button id="btn9">replacewith替换</button>
    <button id="btn10">replaceAll替换</button>
    <button id="btn11">clone复制</button>
    <ul>
        <li>香蕉</li>
        <li class="aa">苹果</li>
        <li>菠萝</li>
    </ul>
    <script>
        $("#btn1").click(function() {
            $("ul").append("<li>榴莲</li>")
        })
        $("#btn2").click(function() {
            $("<li>西瓜</li>").appendTo("ul")
        })
        $("#btn3").click(function() {
            $("ul").prepend("<li>荔枝</li>")
        })
        $("#btn4").click(function() {
            $("<li>哈密瓜</li>").prependTo("ul")
        })
        $("#btn5").click(function() {
            $("ul").before("<h1>水果清单</h1>")
        })
        $("#btn6").click(function() {
            $("ul").after("<span>水果清单</span>")
        })
    </script>
</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值