好一段时间没有更新博文了。刚学习完JS基础知识后,也进入到了JQ的学习。为了能熟练掌握JQ的使用,最好的方法就是反复多练,讲JQ中的API的每一个知识点都练习一遍。若是能作到这个,那么对JQ就没那么陌生了。这一天,先将JQ中的选择器的每一个点熟悉一遍。注:记得先将JQ库引入,而且<script></script>里的代码须要用$(function(){jq代码});在这里,我是没有添加这个。css
1、基本:html
HTML代码:app
<p class="p1">p段落</p> <h class="h1">h标签</h> <div id="div1"> <p class="p2">hehehe</p> </div>
一、jQuery( "*" )查找文档中的每个元素(包括 head, body 等)。ide
$("*").css('background-color','red');
二、jQuery( ".class" )选择给定样式类名的全部元素。布局
$('.h1').css('background-color','yellow');
三、jQuery( "element" )根据给定(html)标记名称选择全部的元素。学习
$('h').css('border','1px solid blue');
四、jQuery( "selector1, selector2, selectorN" )将每个选择器匹配到的元素合并后一块儿返回。动画
$('.h1,.p1,#div1').css('color','blue');
五、jQuery( "#id" )选择一个具备给定id属性的单个元素。this
$('#div1').css({ width:20, height:30, border:'1px solid #ccc', background:'blue' });
2、层级url
HTML代码:spa
<ul class="topnav"> <li class="item1">Item 1</li> <li>Item 2 <ul><li>Nested item 1</li><li>Nested item 2</li><li>Nested item 3</li></ul> </li> <li>Item 3</li> </ul> <label>lll:</label> <input name="name" value="123"/>
一、jQuery( "parent > child" )选择全部指定“parent”元素中指定的"child"的直接子元素
$("ul.topnav > li").css("border", "3px double red");
二、jQuery( "ancestor descendant" 选择给定的祖先元素的全部后代元素。一个元素的后代多是该元素的一个孩子,孙子,曾孙
等。
$('ul li').css('background-color','aquamarine');
三、jQuery( "prev + next" )选择全部紧接在 “prev” 元素后的 “next” 元素
$("label + input").css("color", "blue");
四、jQuery( "prev ~ siblings" )匹配 “prev” 元素以后的全部 兄弟元素。具备相同的父元素,并匹配过滤“siblings”选择器
$('ul ~ label').css('color','yellow');
3、基本筛选:
HTML代码:
<h1>Header 1</h1> <p>Contents 1</p> <h2>Header 2</h2> <button id="run">Run</button> <div></div> <div id="mover"></div> <div></div> <table border="1"> <tr><td>123</td><td>123</td><td>456</td></tr> <tr><td>123</td><td>123</td><td>456</td></tr> <tr><td>123</td><td>123</td><td>456</td></tr> <div lang="en-us"></div> </table>
一、jQuery( ":animated" )选择全部正在执行动画效果的元素.
$("#run").click(function(){ $("div:animated").toggleClass("colored"); }); function animateIt(){ $("#mover").slideToggle("slow", animateIt); } animateIt();
二、jQuery(":eq(index)"),jQuery( ":eq(-index)" )在匹配的集合中选择索引值为index的元素。
index: index为正数时要匹配元素的索引值(从0开始计数),index为负数从最后一个元素开始
倒计数.-1匹配倒数第一个元素
$("td:eq(2)").css("color", "red");
三、jQuery( ":even" ) 选择所引值为偶数的元素,从 0 开始计数。
$("td:even").css("background-color", "#bbbbff");
四、jQuery( ":first" )选择第一个匹配的DOM元素。:first伪类选择器至关于:eq(0)
$("tr:first").css("font-style", "italic");
五、jQuery( ":gt(index)" )选择匹配集合中全部大于给定index(索引值)的元素。
$( "td:gt(4)" ).css( "backgroundColor", "yellow" );
六、jQuery( ":header" )选择全部标题元素,像h1, h2, h3 等.
$(":header").css({ background:'#CCC', color:'blue' });
七、jQuery( ":lang(language)" )选择指定语言的全部元素。
$( "div:lang(en-us)" ).addClass( "usa" );
八、jQuery( ":last" )选择最后一个匹配的元素。
$("tr:last").css({backgroundColor: 'yellow', fontWeight: 'bolder'});
九、jQuery( ":lt(index)" )选择匹配集合中全部索引值小于给定index参数的元素。
$("td:lt(4)").css("color", "red");
十、jQuery( ":not(selector)" )选择全部元素去除不匹配给定的选择器的元素。
$("div:not(#mover)").css('background','red');
十一、jQuery( ":odd" 选择索引值为奇数元素,从 0 开始计数
$("tr:odd").css("background-color", "#bbbbff");
十二、jQuery( ":root" )选择该文档的根元素。
console.log($('p:root'));
4、内容筛选:
一、jQuery( ":contains(text)" ) 选择全部包含指定文本的元素。
<div>George Martin</div> <div>Malcom John Sinclair</div> <div>J. Ohn</div>
$("div:contains('John')").css("text-decoration", "underline");
二、jQuery( ":empty" )选择全部没有子元素的元素(包括文本节点)。须要注意的一件重要的事情是:empty(和 :parent)的子元素包括文本节点。
<tr><td>TD #0</td><td></td></tr> <tr><td>TD #2</td><td></td></tr>
<script>$("td:empty").text("Was empty!").css('background', 'rgb(255,220,200)');</script>
三、jQuery( ":has(selector)" )选择元素其中至少包含指定选择器匹配的一个种元素。
<div><p>Hello in a paragraph</p></div>
<script>$("div:has(p)").addClass("test");</script>
四、jQuery( ":parent" )选择全部含有子元素或者文本的父级元素。
<table border="1"> <tr><td>Value 1</td><td></td></tr> <tr><td>Value 2</td><td></td></tr> </table>
$("td:parent").css('background', 'red');
5、可见性筛选:
一、jQuery( ":hidden" )选择全部隐藏的元素。
元素能够被认为是隐藏的几个状况:
他们的CSS display值是none。
他们是type="hidden"的表单元素。
它们的宽度和高度都显式设置为0。
一个祖先元素是隐藏的,所以该元素是不会在页面上显示。
<span></span> <div></div> <div style="display:none;">Hider!</div> <div></div>
$("div:hidden").show(3000);
二、jQuery( ":visible" )选择全部可见的元素。
若是元素中占据文档中必定的空间,元素被认为是可见的。可见元素的宽度或高度,是大于零。
元素的visibility: hidden 或 opacity: 0被认为是可见的,由于他们仍然占用空间布局。
<button>Show hidden to see they don't change</button> <div></div> <div class="starthidden"></div> <div></div> <div></div> <div style="display:none;"></div>
$("div:visible").click(function () { $(this).css("background", "yellow"); }); $("button").click(function () { $("div:hidden").show("fast"); });
6、属性:
一、jQuery( "[attribute|='value']" )选择指定属性值等于给定字符串或以该字符串为前缀(该字符串后跟一个连字符“-” )的
元素。
attribute: 一个属性名.
value: 一个属性值,引号是可选的.能够是一个有效标识符或带一个引号的字符串。
<div id="mover">123</div>
$('div[id|=mover]').css('color','red');
二、jQuery( "[attribute*='value']" )选择指定属性具备包含一个给定的子字符串的元素。(选择给定的属性是以包含某些值的元
素)
<input name="milkman" /> <input name="letterman2" />
$('input[name*="man"]').val('has man in it!');
三、jQuery( "[attribute~='value']" )选择指定属性用空格分隔的值中包含一个给定值的元素。
<input name="milk man" /> <input name="letterman2" />
$('input[name~="man"]').val('mr. man is in it!');
四、jQuery( "[attribute$='value']" )选择指定属性是以给定值结尾的元素。这个比较是区分大小写的。
<input name="newsletter" />
$('input[name$="letter"]').val('a letter');
五、jQuery( "[attribute='value']" )选择指定属性是给定值的元素。
<input type="radio" name="newsletter" value="Hot Fuzz" />
$('input[value="Hot Fuzz"]').css('background','red');
六、jQuery( "[attribute!='value']" )选择不存在指定属性,或者指定的属性值不等于给定值的元素。
<span>name is newsletter</span> <span>name</span>
$('span[name!="newsletter"]').css('color','red');
七、jQuery( "[attribute^='value']" )选择指定属性是以给定字符串开始的元素
<input name="newsletter" /> <input name="milkman" /> <input name="newsboy" />
$('input[name^="news"]').val('news here!');
八、jQuery( "[attribute]" )选择全部具备指定属性的元素,该属性能够是任何值
<div id="mover">123456</div>
$('div[id]').css('color','red');
九、jQuery( "[attributeFilter1][attributeFilter2][attributeFilterN]" )选择匹配全部指定的属性筛选器的元素
attributeFilter1: 一个属性过滤器.
attributeFilter2: 另外一个属性过滤器, 用于进一步减小被选择的元素。
attributeFilterN: 根据须要有更多的属性过滤器
<input name="milkman" /> <input id="letterman" name="new-letterman" /> <input name="newmilk" />
$('input[id][name$="man"]').val('only this one');
7、子元素筛选:
一、jQuery( ":first-child" )选择全部父级元素下的第一个子元素。
<div> <span>John,</span> <span>Karl,</span> <span>Brandon</span> </div>
$("div span:first-child").css("text-decoration", "underline");
二、jQuery( ":first-of-type" )选择全部相同的元素名称的第一个兄弟元素。
<div> <span>John,</span> <span>Karl,</span> <span>Brandon</span> </div> <script>
$("span:first-of-type").addClass("fot");
三、jQuery( ":last-child" )选择全部父级元素下的最后一个子元素。
<div> <span>John,</span> <span>Karl,</span> <span>Brandon</span> </div>
$("div span:last-child") .css({color:"red", fontSize:"80%"});
四、jQuery( ":last-of-type" )选择的全部元素之间具备相同元素名称的最后一个兄弟元素。
<div> <span>John,</span> <span>Karl,</span> <span>Brandon</span> </div>
$("div span:last-of-type") .css({color:"red", fontSize:"80%"});
五、jQuery( ":nth-child(index/even/odd/equation)" )选择的他们全部父元素的第n个子元素。
<ul> <li>John</li> <li>Karl</li> <li>Brandon</li> </ul>
$("ul li:nth-child(2)").append("<span> - 2nd!</span>");
六、jQuery( ":nth-last-child(index/even/odd/equation)" )选择全部他们父元素的第n个子元素。计数从最后一个元素开始到第
一个
<ul> <li>Dave</li> <li>Rick</li> <li>Timmy</li> <li>Gibson</li> </ul>
$("ul li:nth-last-child(2)").css('color','red');
七、jQuery( ":nth-last-of-type(index/even/odd/equation)" )选择全部他们父级兄弟元素下具备相同的元素名(愚人码头注:标
签名,好比<li>)的倒数第n个子元素,计数从最后一个元素开始到第一个。
<ul> <li>Dave</li> <li>Rick</li> <li>Timmy</li> <li>Gibson</li> </ul>
$("ul li:nth-last-of-type(2)").css('color','red');
八、jQuery( ":nth-of-type(index/even/odd/equation)" )选择同属于一个父元素之下,而且标签名相同的子元素中的第n个。
<div> <span>John</span>, <b>Kim</b>, <span>Adam</span>, <b>Rafael</b>, <span>Oleg</span> </div>
$("ul li:nth-of-type(2)").css('color','red');
九、jQuery( ":only-child" )若是某个元素是其父元素的惟一子元素,那么它就会被选中。若父元素有其余子元素,就不会被匹配
<div> <button>Sibling!</button> </div>
$("div button:only-child")css("border", "2px blue solid");
十、jQuery( ":only-of-type" )选择全部没有兄弟元素,且具备相同的元素名称的元素。若是父元素有相同的元素名称的其余子元
素,那么没有元素会被匹配。
<div> <button>Sibling!</button> </div> <div> <button>Sibling!</button> <button>Sibling!</button> </div>
$("button:only-of-type").css("border", "2px blue solid");
8、表单:
一、jQuery( ":button" )选择全部按钮元素和类型为按钮的元素。
<input type="button" value="Input Button"/> <input type="checkbox" />
$(':button').css('background-color','red');
二、jQuery( "input:checkbox" )选择全部类型为复选框的元素。
<input type="button" value="Input Button"/> <input type="checkbox" />
$('input:button').css('background-color','red');
三、jQuery( ":checked" )匹配全部选中的元素。
<input type="checkbox" name="newsletter" value="Hourly" checked="checked"> <input type="checkbox" name="newsletter" value="Daily">
$('input:checked').css('background-color','red');
四、jQuery( ":disabled" )选择全部被禁用的元素。
<input name="email" disabled="disabled" /> <input name="id" />
$("input:disabled").css('background-color','red');
五、jQuery( ":enabled" )选择全部可用的(未被禁用的)
<input name="email" disabled="enabled" /> <input name="id" />
$("input:enabled").css('background-color','red');
六、jQuery( ":file" )选择全部类型为文件(file)的元素
<input type="file" /> <input type="hidden" />
$("input:file").css('background-color','red');
七、jQuery( ":image" )选择全部图像类型的元素
<input type="image" />
$("input:image").css('width','200');
八、jQuery( ":input" )选择全部 input, textarea, select 和 button 元素.
<input type="password" /> <input type="radio" /> <input type="reset" />
$(":input").css('width','200')
九、jQuery( ":password" )选择全部类型为密码的元素。
<input type="password" />
$("input:password").css('bordr-color','green');
十、jQuery( ":radio" )选择全部类型为radio的元素。
<input type="radio" />
$("input:radio").css('bordr-color','green');
十一、jQuery( ":reset" )选择全部类型为reset的元素。
<input type="reset" />
$("input:reset").css('bordr-color','green');
十二、jQuery( ":selected" )选择全部selected的元素。
<input type="radio" selected /> <input type="radio" />
$("input:selected").css('bordr-color','green');
1三、jQuery( ":submit" )选择全部类型为sumbit的元素。
<input type="submit" selected /> <input type="radio" />
$("input:submit").css('bordr-color','green');
1四、jQuery( ":text" )选择全部类型为text的元素。
<input type="text" /> <input type="radio" />
$("input:text").css('bordr-color','green');