jquery选择器常见操作

获取class1元素下的class2元素

注意 dom取值中间是有空格的

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
</head>
<body>

    <div class="class1">
        <span class="class2">class2</span>
    </div>
    <script>
        var value=$('.class1 .class2').text();
        alert(value)
    </script>
</body>
</html>

选择同时含有class1和class2的元素

注意获取dom时,选择器中间没有空格

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
</head>

<body>


    <span class=" class1 class2">class2</span>

    <script>
        var value = $('.class1.class2').text();
        alert(value)
    </script>
</body>

</html>

选择class1或者class2的元素

注意dom中间是有逗号的

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
</head>

<body>


    <span class=" class1 class2">1</span>
    <span class=" class1 ">2</span>
    <span class="  class2">3</span>

    <script>
        var value = $('.class1,.class2').length;
        alert(value)
    </script>
</body>

</html>

class前缀为pre的所有div的jquery对象

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
</head>

<body>


    <span class="pre-1">1</span>
    <span class=" 1-pre">2</span>
    <span class="  class2">3</span>

    <script>
        var value = $("span[class^='pre']").length;
        alert(value)
    </script>
</body>

</html>

class后缀为class的dom

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
</head>

<body>


    <span class="pre-1">1</span>
    <span class=" 1-last">2</span>
    <span class=" 2-pre">2</span>
    <span class="  class2">3</span>

    <script>
        var value = $("span[class$='last']").length;
        alert(value)
    </script>
</body>

</html>

class中包含pre的dom

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
</head>

<body>


    <span class="pre-1">1</span>
    <span class=" 1-last">2</span>
    <span class=" 2-pre">2</span>
    <span class="  class2">3</span>

    <script>
        var value = $("span[class*='pre']").length;
        alert(value)
    </script>
</body>

</html>
上一篇:全局变量的值为 nil


下一篇:pandas dataframe多层索引取值