Home > Web Front-end > JS Tutorial > jQuery node traverses next, prev, siblings - ipangjie

jQuery node traverses next, prev, siblings - ipangjie

巴扎黑
Release: 2017-06-22 13:10:56
Original
3324 people have browsed it

In

jquery, when we want to get the adjacent elements of an element, there are three commands that can be used:

next(): used to get The next sibling element.

prev(): used to get the previous sibling element.

siblings(): Used to obtain all sibling elements.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>jQuery中的节点遍历next下一个、prev上一个、siblings兄弟</title>

    <script src="js/jquery-1.4.2.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(function() {            //next()方法用于获取节点之后的 挨着 的第一个同辈元素            //参数为空显示挨着 的第一个同辈元素    参数不为空就行匹配,如是参数元素显示,如不是显示空            //$(this).next("p").text()    点击aaaa返回弹出空  点击cccc显示dddd
            $("p").click(function() {
                alert($(this).next("p").text());
            });            //nextAll()nextAll()方法用于获取节点之后的所有同辈元素            //参数为空显示所有同辈元素的值    参数不为空就匹配,如是参数元素显示,如不是显示空            //$(this).nextAll().text() 点击aaaa返回弹出bbbbccccdddd  点击cccc显示dddd            //$(this).nextAll("p").text()    点击aaaa返回弹出ccccdddd  点击cccc显示dddd
            $("p").click(function() {
                alert($(this).nextAll("p").text());
            });            //prev、prevAll兄弟中之前的元素。    与next、nextAll相反
            $("p,p").click(function() {
                alert($(this).prev().text());
                alert($(this).prevAll().text());
                alert($(this).prev("p").text());
                alert($(this).prevAll("p").text());
            });            //siblings()方法用于获取所有同辈元素
            $("p,p").click(function() {
                alert($(this).siblings().text());
                alert($(this).siblings("p").text());
            });
        })        //案例:选中的p变色 $(this).css();$(this).siblings().css()        //案例:评分控件。prevAll,this,nextAll
    </script>

</head>
<body>
    <p>
        aaaa</p>
    <p>
        bbbb</p>
    <p>
        cccc</p>
    <p>
        dddd</p>
</body>
</html>
Copy after login

The above is the detailed content of jQuery node traverses next, prev, siblings - ipangjie. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template