jQuery節點遍歷next下一個、prev上一個、siblings兄弟


<!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("div").text()    點擊aaaa返回彈出空  點擊cccc顯示dddd
            $("div").click(function() {
                alert($(this).next("div").text());
            });

            //nextAll()nextAll()方法用於獲取節點之后的所有同輩元素
            //參數為空顯示所有同輩元素的值    參數不為空就匹配,如是參數元素顯示,如不是顯示空
            //$(this).nextAll().text() 點擊aaaa返回彈出bbbbccccdddd  點擊cccc顯示dddd
            //$(this).nextAll("div").text()    點擊aaaa返回彈出ccccdddd  點擊cccc顯示dddd
            $("div").click(function() {
                alert($(this).nextAll("div").text());
            });

            //prev、prevAll兄弟中之前的元素。    與next、nextAll相反
            $("div,p").click(function() {
                alert($(this).prev().text());
                alert($(this).prevAll().text());
                alert($(this).prev("div").text());
                alert($(this).prevAll("div").text());
            });

            //siblings()方法用於獲取所有同輩元素
            $("div,p").click(function() {
                alert($(this).siblings().text());
                alert($(this).siblings("div").text());
            });
        })
        //案例:選中的p變色 $(this).css();$(this).siblings().css()
        //案例:評分控件。prevAll,this,nextAll
    </script>

</head>
<body>
    <div>
        aaaa</div>
    <p>
        bbbb</p>
    <div>
        cccc</div>
    <div>
        dddd</div>
</body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM