jQuery (two)-jQuery selector attribute filter selector

3. Attribute filter selector

  1. [attribute] matches the element that contains the given attribute.
  2. [attribute=value] matches elements whose given attribute is a certain value
  3. [attribute!=value] matches all elements that do not contain the specified attribute, or the attribute is not equal to a specific value.
  4. [attribute^=value] matches the given attribute is an element that starts with some value
  5. [attribute$=value] matches the given attribute is an element ending with some value
  6. [attribute*=value] Matching a given attribute is an element that contains certain values
  7. [attrSel1] [attrSel2] [attrSelN] Compound attribute selector, used when multiple conditions need to be met at the same time

Note:
[attribute!=value] matches all elements that do not contain the specified attribute or the attribute is not equal to a specific value. It is the complement of 2. [attribute=value] .

That is, $(" div [id !='btn01'] "), which is equivalent to div:not( [id ='btn01'] ), is

  1. All div elements whose id value is not btn01
  2. At the same time, div elements without id attributes will be obtained

That is, the [attribute!=value] selector is equivalent to: not([attr=value])
To match elements with specific attributes but not equal to a specific value, use [attr]:not([attr=value]), Or [attr][attr!=value]. See example 8 below

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jQuery-2021-01-30</title>

    <style type="text/css">
        div,span,p {
     
     
            width: 140px;
            height: 140px;
            margin: 5px;
            background: #aaa;
            border: #000 1px solid;
            float: left;
            font-size: 17px;
            font-family: Verdana;
        }

        div.mini {
     
     
            width: 55px;
            height: 55px;
            background-color: #aaa;
            font-size: 12px;
        }

        div.hide {
     
     
            display: none;
        }
    </style>

    <script type="text/javascript" src="../script/jquery-1.7.2.js"></script>
    <script type="text/javascript">
        //完成如下功能:
            //1.选取含有 属性title 的div元素
            //2.选取 属性title值等于'test'的div元素
            //3.选取 属性title值不等于'test'的div元素(*没有属性title的也将被选中)
            //4.选取 属性title值 以'te'开始 的div元素
            //5.选取 属性title值 以'est'结束 的div元素
            //6.选取 属性title值 含有'es'的div元素
            //7.首先选取有属性id的div元素,然后在结果中 选取属性title值 含有'es'的 div 元素
            //8.选取 含有 title 属性值, 且title 属性值不等于 test 的 div 元素
    </script>

</head>
<body>
<input type="button" value="选取含有 属性title 的div元素." id="btn1"/>
<input type="button" value="选取 属性title值等于'test'的div元素." id="btn2" />
<input type="button"
       value="选取 属性title值不等于'test'的div元素(没有属性title的也将被选中)." id="btn3" />
<input type="button" value="选取 属性title值 以'te'开始 的div元素." id="btn4" />
<input type="button" value="选取 属性title值 以'est'结束 的div元素." id="btn5" />
<input type="button" value="选取 属性title值 含有'es'的div元素." id="btn6" />
<input type="button"
       value="组合属性选择器,首先选取有属性id的div元素,然后在结果中 选取属性title值 含有'es'的 div 元素."
       id="btn7" />
<input type="button"
       value="选取 含有 title 属性值, 且title 属性值不等于 test 的 div 元素." id="btn8" />

<br> <br>
<div class="one" id="one">
    id 为 one,class 为 one 的div
    <div class="mini">class为mini</div>
</div>
<div class="one" id="two" title="test">
    id为two,class为one,title为test的div
    <div class="mini" title="other">class为mini,title为other</div>
    <div class="mini" title="test">class为mini,title为test</div>
</div>
<div class="one">
    <div class="mini">class为mini</div>
    <div class="mini">class为mini</div>
    <div class="mini">class为mini</div>
    <div class="mini"></div>
</div>
<div class="one">
    <div class="mini">class为mini</div>
    <div class="mini">class为mini</div>
    <div class="mini">class为mini</div>
    <div class="mini" title="tesst">class为mini,title为tesst</div>
</div>
<div style="display: none;" class="none">style的display为"none"的div</div>
<div class="hide">class为"hide"的div</div>
<div>
    包含input的type为"hidden"的div<input type="hidden" value="123456789" size="8">
</div>
</body>
</html>

The interface effect is shown in the figure:
Insert picture description here
Function realization: Bind the corresponding click events to each of the 8 buttons

<script type="text/javascript" src="../script/jquery-1.7.2.js"></script>
<script type="text/javascript">
        //完成如下功能:
        $(function () {
     
     
            //1.选取含有 属性title 的div元素
            $("#btn1").click(function () {
     
     
                $("div[title]").css("background","red");
            });
            //2.选取 属性title值等于'test'的div元素
            $("#btn2").click(function () {
     
     
                $("div[title='test']").css("background","red");
            });
            //3.选取 属性title值不等于'test'的div元素(*没有属性title的也将被选中)
            $("#btn3").click(function () {
     
     
                $("div[title!='test']").css("background","red");
            });
            //4.选取 属性title值 以'te'开始 的div元素
            $("#btn4").click(function () {
     
     
                $("div[title^='te']").css("background","red");
            });
            //5.选取 属性title值 以'est'结束 的div元素
            $("#btn5").click(function () {
     
     
                $("div[title$='est']").css("background","red");
            });
            //6.选取 属性title值 含有'es'的div元素
            $("#btn6").click(function () {
     
     
                $("div[title*='es']").css("background","red");
            });
            //7.首先选取有属性id的div元素,然后在结果中 选取属性title值 含有'es'的 div 元素
            $("#btn7").click(function () {
     
     
                $("div[id][title*='es']").css("background","red");
            });
            //8.选取 含有 title 属性值, 且title 属性值不等于 test 的 div 元素
            //和3做个对比
            $("#btn8").click(function () {
     
     
                $("div[title][title!='test']").css("background","red");
            });
        });
</script>

Guess you like

Origin blog.csdn.net/HangHug_L/article/details/113433376