jQuery选择器用法

    原文链接:http://caibaojian.com/jquery-selectors.html

    viajQuery选择器是jQuery库的一大特色,用这些选择器不但可以省去繁琐的javascript 书写方式,还可以节省时间和效率,正是有这些jQuery选择器,才让我们更容易的操作JavaScript的dom。原文来自:http://caibaojian.com/jquery-selectors.html


    1).基本选择器


    ·#id 根据给定的ID匹配一个元素。例如:$("#id")
    ·element 根据给定的元素名匹配所有元素。例如:$("div")
    ·.class 根据给定的类匹配元素。例如:$(".style1");
    ·* 匹配所有元素。例如:$("*")
    ·selector1,selector2,selectorN 将每一个选择器匹配到的元素合并后一起返回。例如:$("#id,div,.style1")


    2).表单选择器


    ·:button 匹配所有按钮。例如:$(":button")
    ·:checkbox 匹配所有复选框。例如:$(":checkbox")
    ·:file 匹配所有文件域。例如:$(":file")
    ·:hidden 匹配所有不可见元素,或者type为hidden的元素。例如:$("input:hidden")
    ·:image 匹配所有图像域。例如:$(":image")
    ·:input 匹配所有 input, textarea, select 和 button 元素。例如:$(":input")
    ·:password 匹配所有密码框。例如:$(":password")
    ·:radio 匹配所有单选按钮。例如:$(":radio")
    ·:reset 匹配所有重置按钮。例如:$(":reset")
    ·:submit 匹配所有提交按钮。例如:$(":submit")
    ·:text 匹配所有的单行文本框。例如:$(":text")
    ·:header 匹配如 h1, h2, h3之类的标题元素。例如:$(":header").css("background", "#EEE");


    2.筛选条件选择器


    1).jQuery属性选择器


    ·[attribute*=value] 匹配给定的属性是以包含某些值的元素。例如:$("input[name*='man'")
    ·[attribute!=value] 匹配所有含有指定的属性,但属性不等于特定值的元素。例如:$(input[name!='man');
    ·[attribute$=value] 匹配给定的属性是以某些值结尾的元素。例如:$("input[name$='man']")
    ·[attribute=value] 匹配给定的属性是某个特定值的元素。例如:$("input[name='man']");
    ·[attribute] 匹配包含给定属性的元素。例如:$("div[id]")
    ·[attribute^=value] 匹配给定的属性是以某些值开始的元素。例如:$("input[name^='man']")
    ·[selector1][selector2][selectorN] 同时满足多个条件。例如:$("input[id][name$='man']")
    ·:hidden 匹配所有的不可见元素。例如:$("tr:hidden")
    ·:visible 匹配所有的可见元素。例如:$("tr:visible")
    ·:checked 匹配所有选中的被选中元素(复选框、单选框等,不包括select中的option)。例如:$("input:checked")
    ·:disabled 匹配所有不可用元素。例如:$("input:disabled")
    ·:enabled 匹配所有可用元素。例如:$("input:enabled")
    ·:selected 匹配所有选中的option元素。例如:$("select option:selected")


    2).jQuery内容选择器


    ·:contains(text) 匹配包含给定文本的元素。例如:$("div:contains('John')")
    ·:empty 匹配所有不包含子元素或者文本的空元素。例如:$("td:empty")
    ·:has(selector) 匹配含有选择器所匹配的元素的元素。例如:$("div:has(p)");
    ·:parent 匹配含有子元素或者文本的元素。例如:$("td:parent")


    3).jQuery层级选择器


    ·ancestor descendant 在给定的祖先元素下匹配所有的后代元素。例如:$("form input")
    ·parent > child 在给定的父元素下匹配所有的子元素。例如:$("form > input")
    ·prev + next 匹配所有紧接在 prev 元素后的 next 元素。例如:$("label + input")
    ·prev ~ siblings 匹配 prev 元素之后的所有 siblings 元素。例如:$("form ~ input")
    ·:first-child 匹配第一个子元素。例如:$("ul li:first-child")
    ·:last-child 匹配最后一个子元素。例如:$("ul li:last-child")
    ·:nth-child(index/even/odd/equation) 匹配其父元素下的第N个子或奇偶元素。例如:$("ul li:nth-child(2)")
    ·:only-child 如果某个元素是父元素中唯一的子元素,那将会被匹配。例如:$("ul li:only-child")


    4).jQuery方法选择器


    ·:animated 匹配所有正在执行动画效果的元素。例如:$("div:animated");
    ·:eq(index) 匹配一个给定索引值的元素。例如:$("tr:eq(1)")
    ·:even 匹配所有索引值为偶数的元素,从 0 开始计数。例如:$("tr:even")
    ·:first 匹配找到的第一个元素。例如:$("tr:first")
    ·:gt(index) 匹配所有大于给定索引值的元素,从 0 开始计数。例如:$("tr:gt(0)")
    ·:last 匹配找到的最后一个元素。例如:$("tr:last")
    ·:lt(index) 匹配所有小于给定索引值的元素。例如:$("tr:lt(2)")
    ·:not(selector) 去除所有与给定选择器匹配的元素。例如:$("input:not(:checked)")
    ·:odd 匹配所有索引值为奇数的元素,从 0 开始计数。例如:$("tr:odd")


    看下面这张表格:


    选择器实例选取
    *$("*")所有元素
    #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 元素


标签: 前端 jquery