1. 程式人生 > >jquery基礎知識2

jquery基礎知識2

use rem classname lin span toggle fff dex alert

1.js和jquery對象的轉換

  js==>jquery對象  $(js對象)

  jquery==>js    jq對象[index]  jq對象.get(index)

技術分享圖片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <ul>
        <li class="item">qing</li>
        <li>qingqing</li>
        <li>qingqingqing
    
</ul> <script src="jquery.js"></script> <script> //js innerText innerHTML value //標簽屬性的操作 .src .href .id .claaName //樣式 .style.屬性 //DOM操作 var datas = ["red","green","yellow"] //1.jquery對象轉化成js對象(js包含jquery) // console.log($("li")[0]);
var item = document.getElementsByClassName("item")[0]; //讓js對象轉換成jquery對象 // console.log($(item)); console.log($(item).css("color","red").click(function(){ alert(1111); })) //鏈式編程 $(item).css("color","red").click(function(){ alert(
111); }) for(var i = 0;i < datas.length;i++){ $("li")[i].style.backgroundColor = datas[i]; } </script> </body> </html>
View Code

2.選擇器:選中標簽對應的jq對象

  jq高級選擇器

技術分享圖片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div class="box">
       <p id="a">qing</p>
        <p>q</p>
        <p>ing</p>
    </div>
    <p>zhang</p>
    <script src="jquery.js"></script>
    <script>
        $(function () {
            // $(".box>p").css("color","green");
            //挨著的下一個兄弟元素
            console.log($("#a+p"));
        })
    </script>
</body>
</html>
View Code

  基本過濾選擇器

技術分享圖片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <ul>
        <li>a</li>
        <li>b</li>
        <li>c</li>
        <li>d</li>
    </ul>
    <script src="jquery.js"></script>
    <script>
        $(function () {
            var i = 2;
            $(`ul li:eq(${i})`).css("color","red");
            $("ul li:first").css("color","red");
            $("ul li:last").css("color","red");
        })
    </script>
</body>
</html>
View Code

  屬性選擇器

技術分享圖片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        input[type="text"]{
            border: none;
            outline: none;
            width: 200px;
            height: 100px;
            font-size: 30px;
            border: 1px solid red;
        }
    </style>
</head>
<body>
    <form action="#">
        <input type="text" name="user" aaa="bbb">
        <input type="password" name="pwd">
        <input type="submit" >
    </form>
    <script src="jquery.js"></script>
    <script type="text/javascript">
        $(function(){
            console.log($("input[type=text]"));
        })
    </script>
</body>
</html>
View Code

  篩選選擇器

技術分享圖片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>
    <div class="father">
        <p>qing</p>
        <a href="#">asd</a>
        <span>qwe</span>
        <div class="g">
            <span>91</span>
        </div>
    </div>
    <script src="jquery.js"></script>
    <script>
        $(function () {
            // console.log($(".father").find(".g"));
            $(".g").click(function () {
                //js對象
                // console.log(this);
                // $(this).css("color","red");
            });
            //find()方法會找到後代無論是子代還是子孫
            // console.log($(".father").find("span"));
            //誰觸發了點擊事件this就是誰
            // $(".father").find(".g>span").click(function () {
            //     console.log(this)
            // })
            //只找子代
            console.log($(".father").children("span"));
            //parent()獲取的是親爹
            console.log($(".g span").parent());
        })
    </script>
</body>
</html>
View Code

3.siblings方法(排他思想)

技術分享圖片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>
<ul>
    <li>a</li>
    <li>b</li>
    <li>c</li>
    <li>d</li>
</ul>
    <script src="jquery.js"></script>
<script>
    $(function () {
        //點元素 變紅色
        $("ul li").click(function () {
            $(this).css("color","red").siblings("li").css("color","black");
        })
    })
</script>
</body>
</html>
View Code

4.選項卡

技術分享圖片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        ul{
            list-style:none;
        }
        a{
            text-decoration: none;
        }
        .box{
            width: 400px;
            height: 300px;

        }
        .box ul {
            width: 100%;
            overflow: hidden;
        }
        .box ul li{
            float: left;
            width: 50px;
            height: 70px;
            margin: 0 10px;
            background-color: red;
            text-align: center;
            line-height: 70px;
        }
        a{
            color: #fff;
        }
        .active{
            width: 100%;
            height: 100px;
            background-color: green;
            display: none;
        }
        </style>
</head>
<body>
    <div class="box">
        <ul>

        </ul>
        <div class="active">

        </div>
        <form action="#">
            <input type="text" value="123">
        </form>
    </div>
    <script src="jquery.js"></script>
    <script>
        $(function () {
            console.log($("input[type=text]").val("111"));
            //初始化操作
            $(".box ul").html(`<li>
                <a href="javascript:void(0);">張三</a>
            </li>
            <li>
                <a href="javascript:void(0);">李四</a>
            </li>
            <li>
                <a href="javascript:void(0);">王五</a>
            </li>
            <li>
                <a href="javascript:void(0);">趙六</a>
            </li>`);
            //點擊a標簽的時候執行的操作
            $(".box ul li a").click(function () {
                $(this).css("color","green").parent("li").siblings("li").find("a").css("color","#fff");
                var textVal = $(this).text();
                var newVal = `<h1>${textVal}</h1>`;
                $(".active").show().html(newVal);
            })
        })
    </script>
</body>
</html>
View Code

5.動畫

技術分享圖片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .box{
            width: 200px;
            height: 200px;
            background-color: red;
        }
    </style>
</head>
<body>
    <button id="btn">動畫</button>
    <div class="box"></div>
    <script src="jquery.js"></script>
    <script>
        $(function () {
            $("#btn").click(function () {
                // $(".box").hide();
                //toggle自己判斷顯隱狀態
                $(".box").stop().toggle(1000);
            })
        })
    </script>
</body>
</html>
View Code

6.自定義動畫

技術分享圖片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        div {
            position: absolute;
            left: 20px;
            top: 30px;
            width: 100px;
            height: 100px;
            background-color: green;
        }
    </style>
</head>
<body>
    <button>動畫</button>
    <div></div>
    <script src="jquery.js"></script>
    <script>
        $(function () {

            // $(‘div‘).animate(
            // {
            //     param1: value1,
            //     param2: value2
            // },
            // speed,
            // function() {
            //     /* stuff to do after animation is complete */
            // });
            $("button").click(function () {
                var json = {"width": 500, "height": 500, "left": 300, "top": 300, "border-radius": 100};
                var json2 = {
                    "width": 100,
                    "height": 100,
                    "left": 100,
                    "top": 100,
                    "border-radius": 100,
                    "background-color": "red"
                };

                //自定義動畫
                $("div").animate(json, 1000, function () {
                    $("div").animate(json2, 1000, function () {
                        alert("動畫執行完畢!");
                    });
                });

            })
        })
    </script>
</body>
</html>
View Code

7.html的標簽屬性操作

技術分享圖片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .tt{
            color: red;
        }
        .active{
            color: green;
        }
    </style>
</head>
<body>
    <div class="box">

    </div>
    <script src="jquery.js"></script>
    <script>
        $(function () {
            //初始化操作
            // $(".box").html("<a href=‘http://www.baidu.com‘>百度一下</a>");
            $(".box").html("<a id=‘anchor‘></a>");
            $("#anchor").attr("href","http://www.baidu.com").text("百度一下");
            console.log($("#anchor").attr("id"));
            $("#anchor").attr({
                title:"123",
                class:"tt"
            });
            $("body").click(function () {
                // $("#anchor").attr("class","active");
                $("#anchor").addClass("active");
                //移除多個類名
                $("#anchor").removeClass("active tt");
                $("#anchor").removeAttr("title href");
            })

        })
    </script>
</body>
</html>
View Code

8.prop標簽對象屬性操作

技術分享圖片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body><input type="radio" name="sex" checked><input type="radio" name="sex">
    <script src="jquery.js"></script>
    <script>
        $(function () {
            // console.log($("input[type=radio]").eq(0).attr("checked"));
            console.dir($("input[type=radio]").eq(1).prop("checked"));
            $("input[type=radio]").eq(1).prop("abc","123");
            console.log($("input[type=radio]").eq(1));
        })
    </script>
</body>
</html>
View Code

9.控制元素顯示隱藏

技術分享圖片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .box{
            width: 200px;
            height: 200px;
            background-color: red;
        }
        .hidden{
            display: none;
        }
    </style>
</head>
<body>
    <button>隱藏</button>
    <div class="box"></div>
    <script src="jquery.js"></script>
    <script>
        $(function () {
            // var isShow = true;
            // $("button").click(function () {
            //     if (isShow){
            //         $(".box").addClass("hidden");
            //         isShow = false;
            //     } else{
            //         $(".box").removeClass("hidden");
            //         isShow = true;
            //     }
            // })

            $("button").click(function () {
                $(".box").toggleClass("hidden");
            })
        })
    </script>
</body>
</html>
View Code

10.選項卡的嵌套

技術分享圖片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        ul{
            list-style: none;
        }
        a{
            text-decoration: none;
        }
        .box{
            width: 400px;
            height: 300px;
        }
        .box ul{
            width: 100%;
            overflow: hidden;
        }
        .box ul li{
            float: left;
            width: 50px;
            height: 70px;
            margin: 0 10px;
            background-color: red;
            text-align: center;
            line-height: 70px;
        }
        a{
            color: #fff;
        }
        .active{
            width: 100%;
            height: 100px;
            background-color: green;
            display: none;
        }
        .show{
            display: block;

        }
    </style>
</head>
<body>
   <button class="next">下一張</button>
    <div class="box">
        <ul>
            <li>
                <a href="javascript:void(0);">張三</a>
            </li>
            <li>
                <a href="javascript:void(0);">李四</a>
            </li>
            <li>
                <a href="javascript:void(0);">王五</a>
            </li>
            <li>
                <a href="javascript:void(0);">趙六</a>
            </li>
        </ul>
        <div class="active"></div>
        <div class="active"></div>
        <div class="active"></div>
        <div class="active"></div>
    </div>
   <script src="jquery.js"></script>
   <script>
       $(function () {
            var count = 0;
            $(".next").click(function () {
                console.log(1)
                count++;
                $(‘ul li‘).eq(count).css(‘backgroundColor‘,‘green‘).siblings(‘li‘).css(‘backgroundColor‘,‘red‘);
                $("div.active").eq(count).addClass("show").html("abc").siblings("div.active").removeClass("show");
            })
            $(".box ul li a").click(function () {
                $(this).css("color","green").parent("li").siblings("li").find("a").css("color","#fff");
                //index()方法獲取索引
                console.log($(this).parent().index());

            })
       })
   </script>
</body>
</html>
View Code

jquery基礎知識2