美文网首页
jQuery-选择器

jQuery-选择器

作者: 测试探索 | 来源:发表于2022-07-02 19:45 被阅读0次

    一、常用选择器

    image.png
    1-1:id选择器,element选择器,.class选择器

    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link rel="stylesheet" href="">
        <script src = "./js/jquery-3.4.1.js" type="text/javascript"></script>
        <script  type="text/javascript">
            //jQuary代码的内容
            $(document).ready(function(){
                //根据id
                var username = $("#username");
                console.log(username);
    
                //根据class来查询
                var areaList = jQuery(".area");
                console.log(areaList);
    
                //根据元素标签来查询
                var pList = $("p");
                console.log(pList)
            })
    
        </script>
    
    </head>
    <body>
        <input type = "text" name="username" id = "username">
    
        <p class="area">广州</p>
        <p>深圳</p>
        <p class="area">长沙</pm>
        <p class="area">北京</p>
    </body>
    </html>
    
    运行结果
    1-2:层级选择器
    image.png

    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link rel="stylesheet" href="">
        <script src = "./js/jquery-3.4.1.js" type="text/javascript"></script>
        <script  type="text/javascript">
            //jQuary代码的内容
            $(document).ready(function(){
                //层级选择器 查询body下的所有元素
                var bodyList = $('body *');
                console.log(bodyList);
    
                //查询body下的所有p元素
                var bodyLists = $('body p');
                console.log(bodyLists);
    
                //查询body下的直接的p子元素
                var bodyLis = $('body > p');
                console.log(bodyLis);
    
                //紧贴之后的元素
                var input = $("label + input");
                console.log(input)
                
    
            })
    
        </script>
    
    </head>
    <body>
        <label for="username"> 用户名</label>
        <input type = "text" name="username" id = "username">
        <input type = "password" name="password" >
    
        <p class="area">广州</p>
        <p>深圳</p>
        <p class="area">长沙</pm>
        <p class="area">北京</p>
    
        <div>
            <p>海南</p>
        </div>
    </body>
    </html>
    
    image.png
    2-3:伪类选择器

    index.html


    image.png
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link rel="stylesheet" href="">
        <script src = "./js/jquery-3.4.1.js" type="text/javascript"></script>
        <script  type="text/javascript">
            //jQuary代码的内容
            $(document).ready(function(){
                //伪类选择器
                var pFirst = $("p:first");
                console.log(pFirst);
    
                //打印p标签第二个元素
                var p2 = $("p:eq(1)");
                console.log(p2);
            })
    
        </script>
    
    </head>
    <body>
        <label for="username"> 用户名</label>
        <input type = "text" name="username" id = "username">
        <input type = "password" name="password" >
    
        <p class="area city">广州</p>
        <p>深圳</p>
        <p class="area">长沙</pm>
        <p class="area">北京</p>
    
        <div>
            <p>海南</p>
        </div>
    </body>
    </html>
    
    运行结果

    相关文章

      网友评论

          本文标题:jQuery-选择器

          本文链接:https://www.haomeiwen.com/subject/ohuqbrtx.html