Home  >  Article  >  Web Front-end  >  How to get sibling elements with jQuery? (code example)

How to get sibling elements with jQuery? (code example)

不言
不言forward
2019-01-18 10:50:532187browse

The content of this article is about how jQuery obtains sibling elements? (Code sample) has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

① $(this).next(); What is obtained is the next sibling element of the current element

②$(this).nextAll(); What is obtained is the current element All the brothers behind the latter

③ $ (this) .prev (); obtained the former brother element of the current element

④ $ (this) .prevall (); What is obtained is all the sibling elements in front of the current element

⑤$(this).siblings(); What is obtained is all the sibling elements of the current element (except itself)

Case exercise:

Requirement analysis: When the mouse enters text, the background of the current text turns red. When clicked, the background color of the text in front of the current text turns yellow, and the background color of the text behind it turns blue. When the mouse moves out, all background colors Cancel the

effect:

The code is as follows:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        ul {
            list-style-type: none;
            width: 200px;
            margin: 100px auto;
        }

        ul li {
            margin-top: 10px;
            cursor: pointer;
            text-align: center;
            font-size: 20px;
        }
    </style>
    <script src="js/jquery-1.12.2.js" type="text/javascript" charset="utf-8"></script>
    <script>
        //获取ul中所有的li,有鼠标进入事件,鼠标离开事件,点击事件
//         $(function () {
//             //获取ul->li
//             $("ul>li").mouseenter(function () {
//                 $(this).css("backgroundColor","red").siblings().css("backgroundColor","");
//             }).mouseleave(function () {
//                 $(this).css("backgroundColor","").siblings().css("backgroundColor","");
//             }).click(function () {
//                 //当前元素前面的所有兄弟元素背景颜色为黄色
//                 //$(this).prevAll().css("backgroundColor","yellow");
//                 //当前元素后面的所有兄弟元素背景颜色为蓝色
//                 //$(this).nextAll().css("backgroundColor","blue");
// 
//                 //链式编程代码
//                 //断链:对象调用方法,返回的不是当前的对象,再调用方法,调用不了,
//                 //解决断链--->恢复到断链之前的一个效果--修复断链
//                 //.end()方法恢复到断链之前的效果
//                 $(this).prevAll().css("backgroundColor","yellow").end().nextAll().css("backgroundColor","blue");
//             });
//         });

            $(function(){
                //链式编程  鼠标进入--鼠标点击--鼠标移出
                //$("ul>li").mouseover().click().mouseout();
                $("ul>li").mouseover(function(){
                    $(this).css("backgroundColor","red").siblings("li").css("backgroundColor","");
                }).click(function(){
                    $(this).prevAll().css("backgroundColor","yellow");
                    $(this).nextAll().css("backgroundColor","blue");
                }).mouseout(function(){
                    $("ul>li").css("backgroundColor","");
                });
            });
    </script>
</head>
<body>
<ul>
    <li>青岛啤酒(TsingTao)</li>
    <li>瓦伦丁(Wurenbacher)</li>
    <li>雪花(SNOW)</li>
    <li>奥丁格教士(Franziskaner)</li>
    <li>科罗娜喜力柏龙(Paulaner)</li>
    <li>嘉士伯Kaiserdom</li>
    <li>罗斯福(Rochefort)</li>
    <li>粉象(Delirium)</li>
    <li>爱士堡(Eichbaum)</li>
    <li>哈尔滨牌蓝带</li>
</ul>

</body>
</html>

Note: The above code Lines 49 and 50 can be compressed into one line, thus introducing a new method

end(); which is used to restore the short chain.

Original two lines of code:

$(this).prevAll().css("backgroundColor","yellow");
$(this).nextAll().css("backgroundColor","blue");

Modified code:

 $(this).prevAll().css("backgroundColor","yellow").end().nextAll().css("backgroundColor","blue");


The above is the detailed content of How to get sibling elements with jQuery? (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete