筛选选择器的功能与过滤选择器有点类似,但是用法不一样,筛选选择器主要是方法。
$("div:first") 直接获得第一个div (永远只能操作第一个)$("div").first() 先获得所有的div,从所有的中筛选出第一个。 (可以操作第一个,也可以操作所有)
一、过滤
eq(index | -index) 类型 :eq()index:正数,从头开始获得指定索引的元素。这个元素的位置是从0算起。0表示第一个-index:负数,从尾开始获得指定索引的元素。-1算起, -1表示最后一个,之前分别是-2,-3…..first() 第一个 :firstlast() 最后一个 :lastis() 判断hasClass() 判断是否是指定class 。返回值true和false <xxx class="my">filter() 筛选出与指定表达式匹配的元素集合not() 将指定内容删除--------------------------------has() 子元素是否有,返回父元素slice(start , end) 截取元素 ,(2,4) --> 2,3
<script type="text/javascript">$(document).ready(function(){//<input type="button" value=" 选择索引值等于3的元素" id="b1"/>$("#b1").click(function(){//$("div").eq(3).css("background-color","red");$("div").eq(-3).css("background-color","red").show();});//<input type="button" value=" 选择第一个div元素" id="b2"/>$("#b2").click(function(){$("div").first().css("background-color","red");});//<input type="button" value=" 选择最后一个div元素" id="b3"/>$("#b3").click(function(){$("div").last().css("background-color","red").show();});//<input type="button" value=" id=one div样式是否是one" id="b4"/>$("#b4").click(function(){//alert($("div#one").hasClass("one"));$("div#one.one").css("background-color","red");});//<input type="button" value=" 选择class为none的所有div" id="b5"/>$("#b5").click(function(){//$("div.none").css("background-color","red").show();$("div").filter(".none").css("background-color","red").show();});//<input type="button" value=" 样式为hide div 下一个兄弟是否是span" id="b6"/>$("#b6").click(function(){//alert($("div.hide+").is("span"));alert($("div.hide").next().is("span"));});//<input type="button" value=" 选择所有div中含有div的" id="b7"/>$("#b7").click(function(){//$("div:has(div)").css("background-color","red");$("div").has("div").css("background-color","red");});//<input type="button" value=" 选择样式为one div 子元素中没有title属性的div(子元素div)" id="b8"/>$("#b8").click(function(){//$("div.one :not([title])").css("background-color","red"); $("div.one").children().not("[title]").css("background-color","red");});//<input type="button" value=" 选择索引号为3,4的div" id="b9"/>$("#b9").click(function(){//$("div:gt(2):lt(2)").css("background-color","red");$("div").slice(3,5).css("background-color","red");});});
示例:
文本框获取和失去焦点
<script type="text/javascript">$(document).ready(function() {//方式一:使用focus和blur方法/*$("input[type='text']").focus(function(){$(this).val("获取焦点");}).blur(function(){$(this).val("失去焦点");}); *///方式二:使用on方法$("input[type='text']").on("focus blur",function(){if($(this).is(":focus")){$(this).val("得到焦点");}else{$(this).val("失去焦点");}});});</script
二、查找
<A><B><C></C><D></D><E></E><F></F></B></A>B.children([...]) 获得所有的子元素。CDEFA.find(D) 从指定的区域查询指定元素。DD.next() 下一个兄弟。ED.nextAll() 后面的所有兄弟。EFE.prev() 上一个兄弟。D (previous)E.prevAll() 前面的所有兄弟。CDE.siblings() 所有兄弟。CDFE.parent() 父元素。BE.closest(A) 向上获得指定的父元素(含自己),如果获得返回一个对象,如果没有没有对象。E.parents() 获得所有的父元素。AB
<script type="text/javascript">$(document).ready(function() {// <input type="button" value=" 选择 id=two 所有子元素" id="b1"/>$("#b1").click(function() {//alert($("#two *").length);$("#two").children().css("background-color","red");});// <input type="button" value=" 选择 id=two 子元素title=other 元素 " id="b2"/>$("#b2").click(function() {//$("#two>[title='other']").css("background-color","red");$("#two").children("[title='other']").css("background-color","red");});// <input type="button" value=" 选择 id=two 下一个兄弟" id="b3"/>$("#b3").click(function() {//$("#two+").css("background-color","red");$("#two").next().css("background-color","red");});// <input type="button" value=" 选择 id=two 后面的所有兄弟" id="b4"/>$("#b4").click(function() {$("#two").nextAll().css("background-color","red");});// <input type="button" value=" 选择 id=two 上一个兄弟" id="b5"/>$("#b5").click(function() {$("#two").prev().css("background-color","red");});// <input type="button" value=" 选择 id=two 前面的所有兄弟" id="b6"/>$("#b6").click(function() {$("#two").prevAll().css("background-color","red");});// <input type="button" value=" 选择 id=two 所有兄弟" id="b7"/>$("#b7").click(function() {$("#two").siblings().css("background-color","red");});// <input type="button" value=" 选择 id=two 父元素" id="b8"/>$("#b8").click(function() {$("#two").parent().css("background-color","red");});});</script>
示例:
QQ分组
<script type="text/javascript">$(document).ready(function(){//开始所有的a标签默认隐藏$("div").children("a").hide().prev("span").click(function(){//1.切换隐藏显示if($(this).nextAll().is(":hidden")){$(this).nextAll().show();}else{$(this).nextAll().hide();}//2.点击当前展开当前,自动关闭其他//$(this).nextAll().show().parent().siblings("div").children("a").hide();});});</script>
三、串联
A.add(B) 将A和B组合一个对象 。类似 $("A,B")end() :回到最近的一个操作之前A.children().children().end() .end()A 孩子 孙子 孩子 A-----------------------contents() 获得所有的子节点(子元素 和 文本)
<script type="text/javascript">$(document).ready(function() {// <input type="button" value=" 选择 id=one和two 的div" id="b1"/>$("#b1").click(function(){//$("#one,#two").css("background-color","red");$("#one").add($("#two")).css("background-color","red");});// <input type="button" value=" 选择id=one 所有的孩子,以及one自己" id="b2"/>$("#b2").click(function(){//$("#one>*,#one").css("background-color","red");$("#one").children().add("#one").css("background-color","red");});// <input type="button" value=" 选择id=one 所有的孩子,设置颜色为红,设置one自己为黄" id="b3"/>$("#b3").click(function(){$("#one").children().css("background-color","red").end().css("background-color","yellow");});// <input type="button" value=" 获得id=two 子节点个数" id="b4"/>$("#b4").click(function(){alert($("#two").contents().length);});//注意:这里面打印结果为5,原因有默认的换行的文本节点存在});</script>
示例:
获取兄弟元素
<ul><li>青岛啤酒(TsingTao)</li><li>瓦伦丁(Wurenbacher)</li><li>雪花(SNOW)</li><li>奥丁格教士(Franziskaner)</li><li>科罗娜喜力柏龙(Paulaner)</li><li>嘉士伯Kaiserdom</li><li>罗斯福(Rochefort)</li><li>粉象(Delirium)</li><li>爱士堡(Eichbaum)</li><li>哈尔滨牌蓝带</li></ul><style>ul {list-style-type: none;width: 200px;position: absolute;left: 500px;}ul li {margin-top: 10px;cursor: pointer;text-align: center;font-size: 20px;}</style><script>//获取ul中所有的li,有鼠标进入事件,鼠标离开事件,点击事件$(function () {//获取ul->li$("ul>li").mouseenter(function () {$(this).css("backgroundColor","red").siblings().css("backgroundColor","");}).mouseleave(function () {$(this).css("backgroundColor","").siblings().css("backgroundColor","");}).click(function () {//当前元素前面的所有兄弟元素背景颜色为黄色//$(this).prevAll().css("backgroundColor","yellow");//当前元素后面的所有兄弟元素背景颜色为蓝色//$(this).nextAll().css("backgroundColor","blue");//链式编程代码//断链:对象调用方法,返回的不是当前的对象,再调用方法,调用不了,//解决断链--->恢复到断链之前的一个效果--修复断链//.end()方法恢复到断链之前的效果$(this).prevAll().css("backgroundColor","yellow").end().nextAll().css("backgroundColor","blue");});});</script>
五星评分
<ul class="comment"><li>☆</li><li>☆</li><li>☆</li><li>☆</li><li>☆</li></ul><style>* {padding: 0;margin: 0;}.comment {font-size: 40px;color: yellow;}.comment li {float: left;}ul {list-style: none;}</style><script>$(function () {$(".comment>li").mouseover(function () {$(this).text("★").prevAll("li").text("★").end().nextAll("li").text("☆");}).mouseout(function () {$(".comment").find("li").text("☆");//鼠标移出的时候让所点击的li之前的所有li选中$(".comment>li[index=1]").text("★").prevAll("li").text("★")}).click(function () {//点击li的时候给当前li绑定一个属性值用于鼠标移出时候记录哪些li需要选中$(this).attr("index","1").siblings("li").removeAttr("index");});});</script>
