zoukankan      html  css  js  c++  java
  • jquery,选择器

    http://www.w3school.com.cn/jquery/jquery_ref_selectors.asp 

     例子1,给<P>元素添加onclick事件

    Var items=document.getElementsByTagName(“p”);//获取网页所有p元素

    Forvar i=0i<items.length;i++){

       Items[i].onclick=function(){

    //onclick事件

    }

    例子1

    $(“p”).click(function(){

    //onclick事件

    })

     

    例子2,使一个特定的表格隔行变色

    Var item=document.getElementById(“tb”);// tb是表格id <table id=”tb”>

    Var tbody=item.getElementByTagName(“tbody”)[0];//获取表格第一个tbody元素

    Var trs=tbody.getElementsByTagName(“tr”); //获取tbody下面的所有tr元素集合

    Forvar i=0i<trs.length;i++{

    If(i%2==0)//取模 (取余数 例如 0%2==0 1%2==1

    {

    Trs[i].style.backgroundColor=”#888”;//改变符合条件的tr元素的背景色

    }

    }

    例子2

    $(‘#tb tbody tr : even’).css(“backgroundColor”,”#888”);

    //获得idtb的元素,然后寻找它下面的tbody标签,再寻找tbody下面的索引值是偶数的tr元素  

    //改变背景色 css(“property”,”value”);

     

    例子3 对多选框进行操作,输出选中的多选框个数

    Var btn =document.getElemengtById(“btn”);

    Ntn.onclick=function(){

    Var arrays=new Array();

    Var items=document.getElementByName(“check”);//获取namecheck的控件

    For(int i=0;i<items.length;i++){

    If(items[i].checked)

    {

       Arrays.puth(items[i].value);

     

    }

    }

    Alert(“选中个数为:”+ arrays.length);

    }

    例子3

    $(‘#btn’).click(function(){

    Var length=$(“input[name=’check’]:checked”).length;

    Alert(“选中的个数为”+length)

    })

    基本选择器

    根据id     #id     $(#test)  返回单个元素          document.getElementById(test)  

    根据class .class    $(.test) 返回集合元素

    根据标签   element   $(“p”) 返回集合元素 

    所有元素      *      $(*)        返回集合元素

    基本选择器示例

    eg1: //选择 id one 的元素

          $('#btn1').click(function(){

             $('#one').css("background","#bfa"); });

    eg2: //选择 class mini 的所有元素

     $('#btn2').click(function(){

             $('.mini').css("background","#bfa");});

     eg3: //选择 元素名是 div 的所有元素

     $('#btn3').click(function(){

             $('div').css("background","#bfa"); });

    eg4: //选择 所有的元素

     $('#btn4').click(function(){

             $('*').css("background","#bfa");});

    eg5: //选择 所有的span元素和idtwodiv元素

     $('#btn5').click(function(){

             $('span,#two').css("background","#bfa"); });

    层次选择器

    1, 选取元素后代 $(“div span”) 选取div元素里所有的span元素

    2, 选取元素后代 $(“div >span”) 选取div元素下所有的span元素,指div的次级子元素

    <div><span></span></div> , <div><body><span></span></ body ></div>这样div下面就不能获取到span

    3, $(‘prev + next ’)选取紧接在prev元素后面的next元素  

    $(‘.one + div’) 选择classone的下一个div元素 相当于$(“.one”).next(“div”);

    4, $(‘prev ~ sibling ’) 选取prev元素之后的所有sibing

    $(#two~ div)选取idtwo的元素后面的所有div兄弟元素相当于$(“. two”).nextAll(“div”);

    层次选择器示例

     $(document).ready(function(){//选择 body内的所有div元素.

          $('#btn1').click(function(){

             $('body div').css("background","#bbffaa");})

     //body内的选择 元素名是div 的子元素.

     $('#btn2').click(function(){

             $('body > div').css("background","#bbffaa");})

     //选择 所有classone 的下一个div元素.

     $('#btn3').click(function(){

             $('.one + div').css("background","#bbffaa");})

     //选择 idtwo的元素后面的所有div兄弟元素.

     $('#btn4').click(function(){

               $('#two ~ div').css("background","#bbffaa");})});

      <script type="text/javascript">

            $(function() {

                //实现隔行变色

                $("tr:odd", "#mydiv").css("background-color", "yellow");

                $("tr:has(td)", "#mydiv").mouseover(function() {

                //获取到有td这个标签的tr标签

    //has将匹配元素集合缩减为包含特定元素的后代的集合

                    if ($(this).find(":checkbox")[0].checked) return false;

    //如果当前行 checkbox被选中

                    this.tag = $(this).css("background-color");

                    $(this).css("background-color", "red");

                })

                .mouseout(function() {

                    if ($(this).find(":checkbox")[0].checked) return false;

                    if (this.tag)

                        $(this).css("background-color", this.tag);

                })

                $(":checkbox", "#mydiv").click(

    //选取#mydiv范围内所有checkbox

                   function() {

                       if (this.checked) {

                           //$(this).parent().parent()

    // $(this) 指选中的td元素

                           $(this).parents("tr").css("background-color", "blue");

                       }

                   }

                )

             }

           )

        </script>

    基本选择器

    根据id     #id     $(#test)  返回单个元素          document.getElementById(test)  

    根据class .class    $(.test) 返回集合元素

    根据标签   element   $(“p”) 返回集合元素 

    所有元素      *      $(*)        返回集合元素

    基本选择器示例

    eg1: //选择 id one 的元素

          $('#btn1').click(function(){

             $('#one').css("background","#bfa"); });

    eg2: //选择 class mini 的所有元素

     $('#btn2').click(function(){

             $('.mini').css("background","#bfa");});

     eg3: //选择 元素名是 div 的所有元素

     $('#btn3').click(function(){

             $('div').css("background","#bfa"); });

    eg4: //选择 所有的元素

     $('#btn4').click(function(){

             $('*').css("background","#bfa");});

    eg5: //选择 所有的span元素和idtwodiv元素

     $('#btn5').click(function(){

             $('span,#two').css("background","#bfa"); });

    层次选择器

    1, 选取元素后代 $(“div span”) 选取div元素里所有的span元素

    2, 选取元素后代 $(“div >span”) 选取div元素下所有的span元素,指div的次级子元素

    <div><span></span></div> , <div><body><span></span></ body ></div>这样div下面就不能获取到span

    3, $(‘prev + next ’)选取紧接在prev元素后面的next元素  

    $(‘.one + div’) 选择classone的下一个div元素 相当于$(“.one”).next(“div”);

    4, $(‘prev ~ sibling ’) 选取prev元素之后的所有sibing

    $(#two~ div)选取idtwo的元素后面的所有div兄弟元素相当于$(“. two”).nextAll(“div”);

    层次选择器示例

     $(document).ready(function(){//选择 body内的所有div元素.

          $('#btn1').click(function(){

             $('body div').css("background","#bbffaa");})

     //body内的选择 元素名是div 的子元素.

     $('#btn2').click(function(){

             $('body > div').css("background","#bbffaa");})

     //选择 所有classone 的下一个div元素.

     $('#btn3').click(function(){

             $('.one + div').css("background","#bbffaa");})

     //选择 idtwo的元素后面的所有div兄弟元素.

     $('#btn4').click(function(){

               $('#two ~ div').css("background","#bbffaa");})});

      <script type="text/javascript">

            $(function() {

                //实现隔行变色

                $("tr:odd", "#mydiv").css("background-color", "yellow");

                $("tr:has(td)", "#mydiv").mouseover(function() {

                //获取到有td这个标签的tr标签

    //has将匹配元素集合缩减为包含特定元素的后代的集合

                    if ($(this).find(":checkbox")[0].checked) return false;

    //如果当前行 checkbox被选中

                    this.tag = $(this).css("background-color");

                    $(this).css("background-color", "red");

                })

                .mouseout(function() {

                    if ($(this).find(":checkbox")[0].checked) return false;

                    if (this.tag)

                        $(this).css("background-color", this.tag);

                })

                $(":checkbox", "#mydiv").click(

    //选取#mydiv范围内所有checkbox

                   function() {

                       if (this.checked) {

                           //$(this).parent().parent()

    // $(this) 指选中的td元素

                           $(this).parents("tr").css("background-color", "blue");

                       }

                   }

                )

             }

           )

    </script>

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    过滤选择器

    :first

    $("p:first")

    第一个 <p> 元素,返回单个元素

    :last

    $("p:last")

    最后一个 <p> 元素,返回单个元素

    :not(selector)

    $("input:not(:empty)")

    所有不为空的 input 元素,返回集合元素

    去除所有与给定选择器匹配的元素

    :even

    $("tr:even")

    所有索引偶数 <tr> 元素,返回集合元素

    $(“input:even”),选取索引为偶数的input元素

    :odd

    $("tr:odd")

    所有索引奇数 <tr> 元素,返回集合元素

    $(“input:odd”),选取索引为奇数的input元素

    :eq(index)

    $("ul li:eq(3)")

    列表中的第四个元素(index 0 开始),返回单个元素

    $(“input:eq(1)”)选取索引等于1input元素

    :gt(no)

    $("ul li:gt(3)")

    列出 index 大于 3 的元素,返回集合元素

    $(“input:gt(1)”)选取索引大于1input元素

    :lt(no)

    $("ul li:lt(3)")

    列出 index 小于 3 的元素,返回集合元素

    $(“input: lt (1)”)选取索引小于1input元素

    :header

    $(":header")

    所有标题元素 <h1> - <h6>

    $(":header"),选取网页所有<h1><h2>…

    :animated

     

    所有动画元素

    $(“div:animated”)选取正在执行动画的div元素

    通过索引访问集合元素

        //选择第一个div元素.

          $('#btn1').click(function(){  $('div:first').css("background","#bfa"); })

     //选择最后一个div元素.

     $('#btn2').click(function(){ $('div:last').css("background","#bfa"); })

     //选择class不为one 所有div元素.

     $('#btn3').click(function(){  $('div:not(.one)').css("background","#bfa"); })

     //选择 索引值为偶数 div元素。

     $('#btn4').click(function(){

    $('div:even').css("background","#bfa"); })

                  

    //选择 索引值为奇数 div元素。

     $('#btn5').click(function(){

             $('div:odd').css("background","#bfa");

     })

     //选择 索引等于 3 的元素

     $('#btn6').click(function(){

             $('div:eq(3)').css("background","#bfa");

     })

     //选择 索引大于 3 的元素

     $('#btn7').click(function(){

             $('div:gt(3)').css("background","#bfa");

     })

     //选择 索引小于 3 的元素

     $('#btn8').click(function(){

             $('div:lt(3)').css("background","#bfa");

     })

       //选择 所有的标题元素.比如h1, h2, h3等等...

     $('#btn9').click(function(){

             $(':header').css("background","#bfa");

     })

     //选择 当前正在执行动画的所有元素.

     $('#btn10').click(function(){

             $(':animated').css("background","#bfa");

     });

    内容过滤选择器

    :contains(text)

    $(":contains('W3School')")

    包含指定字符串的所有元素,返回集合元素

    $(“div:contains(‘’)”)选取含有文本“我”的div元素

    :empty

    $(":empty")

    无子(元素)节点的所有元素,返回集合元素

    $(“div:empty”)选取不包含子元素(包含文本)div

    :has(selector)

    选取含有选择器所匹配的元素的元素

    $(“div:has(p)”)选取含有<p>元素的div元素

    :parent

    选取含有子元素或文本的元素

    $(“div:parent”)选取拥有子元素(包含文本元素)的div元素

     

     

    内容过滤选择器示例

    $(document).ready(function(){

     //选取含有文本"di"div元素.

          $('#btn1').click(function(){

             $('div:contains(di)').css("background","#bbffaa");

     })

     //选取不包含子元素(或者文本元素)div空元素.

     $('#btn2').click(function(){

             $('div:empty').css("background","#bbffaa");

     })

     //选取div元素子元素含有classmini元素

     $('#btn3').click(function(){

             $('div:has(.mini)').css("background","#bbffaa");

     })

     //选取含有子元素(或者文本元素)div元素.

     $('#btn4').click(function(){

             $('div:parent').css("background","#bbffaa");})

     });

    :hidden

    $("p:hidden")

    所有隐藏的 <p> 元素

    包括<input type=”hidden”><div style=”display:none”> <div style=”visibility:hidden”;等元素,如果只想选取input元素,可以使用$(“input:hidden”)

    :visible

    $("table:visible")

    所有可见的表格

    $(“div:visible”)选取所有可见的div元素

    可见性过滤选择器示例

     $('#reset').click(function(){

          window.location.reload();})

          //idmover的元素添加动画.

       function animateIt() {

             $("#mover").slideToggle("slow", animateIt); }//回调函数

            animateIt();

            //选取所有不可见的元素.包括<input type="hidden"/>.

          $('#btn_hidden').click(function(){

          alert( "不可见的元素有:"+$('body :hidden').length +"!\n"+

               "其中不可见的div元素有:"+$('div:hidden').length+"!\n"+

               "其中文本隐藏域有:"+$('input:hidden').length+"!");

             $('div:hidden').show(3000).css("background","#bbffaa"); })

     //选取所有可见的元素.

     $('#btn_visible').click(function(){

             $('div:visible').css("background","#FF6500"); })

    }        注意:在可见性选择器中,需要注意:hidden,它不仅包括 样式属性displaynone的元素,还包括 文本隐藏域(<input type=“hidden”/>)Visibility:hidden的元素

    属性过滤选择器

     [attribute]

    选取拥有此属性的元素

    $(“div[id]”) 选取拥有属性id的元素

    [attribute=value]

    选取属性值为value的元素

    $(“div[title=test]”)选取属性titletestdiv元素

    [attribute!=value]

    选取属性值不等于value的元素

    $(“div[title!=test]”)选取属性title不等于testdiv元素(没有属性titlediv也会被选)

    [attribute^=value]

    选取属性值以value开始的元素

    $(“div[title^=test]”)选取属性title”test”开始的元素

    [attribute$=value]

    选取属性值以value结束的元素

    $(“div[title$=test]”)选取属性title”test”结束的元素

    [attribute*=value]

    选取属性值含有value的元素

    $(“div[title*=test]”)选取属性title含有”test”的元素

    [selector1] [selector2] [selectorN]

    用属性选择器合并成一个复合属性选择器,满足多个条件

    $(“div[id][title$=’test’]”)选择拥有属性id,并属性title以‘test’结束的div元素

    属性过滤选择器示例

     //选取含有 属性title div元素.

          $('#btn1').click(function(){

             $('div[title]').css("background","#bbffaa");

     })

     //选取 属性title值等于 test div元素.

     $('#btn2').click(function(){

             $('div[title=test]').css("background","#bbffaa");})

     //选取 属性title值不等于 test div元素.

     $('#btn3').click(function(){

             $('div[title!=test]').css("background","#bbffaa");

     })

     //选取 属性title te 开始 div元素.

     $('#btn4').click(function(){

             $('div[title^=te]').css("background","#bbffaa");

     })

     //选取 属性title est 结束 div元素.

     $('#btn5').click(function(){

             $("div[title$=est]").css("background","#bbffaa");})

     //选取 属性title 含有 es div元素.

     $('#btn6').click(function(){

             $("div[title*=es]").css("background","#bbffaa");})

    //组合属性选择器,首先选取有属性iddiv元素,然后在结果中 选取属性title 含有 es 的元素.

     $('#btn7').click(function(){

             $("div[id][title*=es]").css("background","#bbffaa"); })

    子元素过滤选择器

    :nth-child(index/even/

    odd/equation)

    选取每个父元素下的index个子元素

    或机偶元素(index1算起)

    :eq(index)只匹配一个元素。而:nth-clild将为每一个父元素匹配子元素。并且:nth-clild(index)index是从1算起,eqindex是从0算起

     

    :first-child

    选取每个父元素的第一个子元素

    :first只返回单个元素,而:first-child选择将为每个父元素匹配第一个子元素。例如$(“ul li:first-child”)选取每个<ul>中的第一个<li>元素

    可能有多个ul元素,返回每个ul元素的第一个li

    :last-child

    选取每个父元素的最后一个子元素

    :list只返回单个元素,而:list-child选择符将为每个父元素匹配最后一个子元素

    例如$(“ul li:last-chlid”)<ul>中选取最后一个<li>元素

    :only-child

    如果某个父元素是它父元素中唯一 子元素,那么将会被匹配,如果父元素中有其他子元素,则不被匹配

    $(“ul li:only-chlid”)<ul>中选取唯一一个<li>元素

    :nth-child(even)能选取每个父元素下的索引值是偶数的元素

    :nth-child(odd)能选取每个父元素下的索引值是奇数的元素

    :nth-child(2) 能选取每个父元素下索引值等于2的元素

    :nth-child(3n) 能选取每个父元素下索引值是3的倍数的元素(n0开始)

    :nth-child(3n+1) 能选取每个父元素下索引值是3n+1的元素(n0开始)

    子元素过滤器示例

     //选取每个父元素下的第2个子元素 div等于one

          $('#btn1').click(function(){

             $('div.one :nth-child(2)').css("background","#bbffaa");

     })

     //选取每个父元素下的第一个子元素

     $('#btn2').click(function(){

             $('div.one :first-child').css("background","#bbffaa");

     })

     //选取每个父元素下的最后一个子元素

     $('#btn3').click(function(){

             $('div.one :last-child').css("background","#bbffaa");

     })

     //如果父元素下的仅仅只有一个子元素,那么选中这个子元素

     $('#btn4').click(function(){

             $('div.one :only-child').css("background","#bbffaa");

     })

     

     

    表单属性过滤选择器

    :enabled

    选取所有可用元素

    $(“#from1 :enabled”) 选取id我为from1的表单所有可用元素

    :disabled

    选取所有不可用元素

    $(“#from2 :disabled”) 选取id我为from2的表单所有不可用元素

    :checked

    选择所有被选中元素(单选框,复选框)

    $(input :checked)选取所有被选中的input元素

    :selected

    选择所有被选中的选项元素(下拉列表)

    $(select :selected)选取所有被选中的选项元素

    <script type="text/javascript">

     $(document).ready(function(){//重置表单元素

         $(":reset").click(function(){

            setTimeout(function() {

               countChecked();

               $("select").change();

            },0); });

         //对表单内 可用input 赋值操作.

          $('#btn1').click(function(){

            $("#form1 input:enabled").val("这里变化了!"); 

            return false; })

         //对表单内 不可用input 赋值操作.

         $('#btn2').click(function(){

            $("#form1 input:disabled").val("这里变化了!");

            return false; })

         //使用:checked选择器,来操作多选框.

           $(":checkbox").click(countChecked);

           function countChecked() {

            var n = $("input:checked").length;

            $("div").eq(0).html("<strong>有"+n+" 个被选中!</strong>");}

           countChecked();//进入页面就调用.

         //使用:selected选择器,来操作下拉列表.

           $("select").change(function () {

                var str = "";

                $("select :selected").each(function () {//选中项执行事件

                      str += $(this).text() + ",";

                });

                $("div").eq(1).html("<strong>你选中的是:"+str+"</strong>");

            }).trigger('change');

           // trigger('change') 在这里的意思是:

           // select加载后,马上执行onchange.

           // 也可以用.change()代替.

     });

     //]]>

     </script>

    表单选择器

    :input

    $(":input")

    所有 <input> 元素,<input>,<textarea>,<select>,<button>

    :text

    $(":text")

    所有 type="text" <input> 元素,所有单行文本框

    :password

    $(":password")

    所有 type="password" <input> 元素,所有密码框

    :radio

    $(":radio")

    所有 type="radio" <input> 元素,单选框

    :checkbox

    $(":checkbox")

    所有 type="checkbox" <input> 元素,复选框

    :submit

    $(":submit")

    所有 type="submit" <input> 元素,所有提交按钮

    :reset

    $(":reset")

    所有 type="reset" <input> 元素。所有重置按钮

    :button

    $(":button")

    所有 type="button" <input> 元素,所有按钮

    :image

    $(":image")

    所有 type="image" <input> 元素,所有图像按钮

    :file

    $(":file")

    所有 type="file" <input> 元素。

    var $alltext = $("#form1 :text");

        var $allpassword= $("#form1 :password");

        var $allradio= $("#form1 :radio");

        var $allcheckbox= $("#form1 :checkbox");

        var $allsubmit= $("#form1 :submit");

        var $allimage= $("#form1 :image");

        var $allreset= $("#form1 :reset");

    var $allbutton= $("#form1 :button"); // <input type=button />  <button ></button>都可以匹配

        var $allfile= $("#form1 :file");

        var $allhidden= $("#form1 :hidden"); // <input type="hidden" /><div style="display:none">test</div>都可以匹配.

        var $allselect = $("#form1 select");

        var $alltextarea = $("#form1 textarea");

        var $AllInputs = $("#form1 :input");

    var $inputs = $("#form1 input"); 

    jQuery 选择器

    选择器实例选取
    *$("*")所有元素
    #id$("#lastname")id="lastname" 的元素
    .class$(".intro")所有 class="intro" 的元素
    element$("p")所有 <p> 元素
    .class.class$(".intro.demo")所有 class="intro" 且 class="demo" 的元素
       
    :first$("p:first")第一个 <p> 元素
    :last$("p:last")最后一个 <p> 元素
    :even$("tr:even")所有偶数 <tr> 元素
    :odd$("tr:odd")所有奇数 <tr> 元素
       
    :eq(index)$("ul li:eq(3)")列表中的第四个元素(index 从 0 开始)
    :gt(no)$("ul li:gt(3)")列出 index 大于 3 的元素
    :lt(no)$("ul li:lt(3)")列出 index 小于 3 的元素
    :not(selector)$("input:not(:empty)")所有不为空的 input 元素
       
    :header$(":header")所有标题元素 <h1> - <h6>
    :animated 所有动画元素
       
    :contains(text)$(":contains('W3School')")包含指定字符串的所有元素
    :empty$(":empty")无子(元素)节点的所有元素
    :hidden$("p:hidden")所有隐藏的 <p> 元素
    :visible$("table:visible")所有可见的表格
       
    s1,s2,s3$("th,td,.intro")所有带有匹配选择的元素
       
    [attribute]$("[href]")所有带有 href 属性的元素
    [attribute=value]$("[href='#']")所有 href 属性的值等于 "#" 的元素
    [attribute!=value]$("[href!='#']")所有 href 属性的值不等于 "#" 的元素
    [attribute$=value]$("[href$='.jpg']")所有 href 属性的值包含以 ".jpg" 结尾的元素
       
    :input$(":input")所有 <input> 元素
    :text$(":text")所有 type="text" 的 <input> 元素
    :password$(":password")所有 type="password" 的 <input> 元素
    :radio$(":radio")所有 type="radio" 的 <input> 元素
    :checkbox$(":checkbox")所有 type="checkbox" 的 <input> 元素
    :submit$(":submit")所有 type="submit" 的 <input> 元素
    :reset$(":reset")所有 type="reset" 的 <input> 元素
    :button$(":button")所有 type="button" 的 <input> 元素
    :image$(":image")所有 type="image" 的 <input> 元素
    :file$(":file")所有 type="file" 的 <input> 元素
       
    :enabled$(":enabled")所有激活的 input 元素
    :disabled$(":disabled")所有禁用的 input 元素
    :selected$(":selected")所有被选取的 input 元素
    :checked$(":checked")所有被选中的 input 元素
  • 相关阅读:
    FFT入门
    FJOI省队集训 chessboard
    FJOI省队集训 florida
    树上莫队
    NOIP2015 Revenge
    APIO2013 tasksauthor
    油漆门
    一些字符串有关的题目
    字符串题模板集合
    sg函数与博弈论2
  • 原文地址:https://www.cnblogs.com/aqbyygyyga/p/2232116.html
Copyright © 2011-2022 走看看