how2j.cn


工具版本兼容问题
筛选器指的是在已经通过选择器选中了元素后,在此基础上进一步选择。

关键字 简介 示例代码
first()
last()
eq(num)
第一个 最后一个 第几个
示例代码
parent()
parents()
父 祖先
示例代码
children()
find()
儿子 后代
示例代码
siblings()
同级
示例代码
示例 1 : 第一个 最后一个 第几个   
示例 2 : 父 祖先   
示例 3 : 儿子 后代   
示例 4 : 同级   

示例 1 :

第一个 最后一个 第几个

首先通过 $("div") 选择了多个div元素,接下来做进一步的筛选
first() 第1个元素
last() 最后一个元素
eq(num) 第num个元素
注: num基0
"); window.frames["iframe_show1247"].document.write(decodeHtml($("textarea#stepcodeTextarea1247").val())); window.frames["iframe_show1247"].document.close(); $(window.frames["iframe_show1247"]).load(function(){ $("#iframe_show1247").height($("#iframe_show1247").contents().find("body").height()+showittryitheight); }); $("#iframe_show1247").height($("#iframe_show1247").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/jquery.min.js"></script> <script> $(function(){ $("#b1").click(function(){ $("div").first().toggleClass("pink"); }); $("#b2").click(function(){ $("div").last().toggleClass("pink"); }); $("#b3").click(function(){ $("div").eq(4).toggleClass("pink"); }); }); </script> <button id="b1">切换第1个div背景色</button> <button id="b2">切换最后1个div背景色</button> <button id="b3">切换第5个div背景色</button> <br> <br> <style> .pink{ background-color:pink; } </style> <div> <span>Hello JQuery 1</span> </div> <div > <span>Hello JQuery 2</span> </div> <div > <span>Hello JQuery 3</span> </div> <div > <span>Hello JQuery 4</span> </div> </div> <div > <span>Hello JQuery 5</span> </div> <div > <span>Hello JQuery 6</span> </div>
"); window.frames["iframe1247"].document.write(decodeHtml(code1247)); window.frames["iframe1247"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1247"]).load(function(){ $("#iframe1247").height($("#iframe1247").contents().find("body").height()+showittryitheight); }); $("#iframe1247").height($("#iframe1247").contents().find("body").height()+showittryitheight); alreadyWriteCode1247 = code1247; $("#rendering1247").hide(); $("#rendered1247").show(); } var tRereshRetry2DemoPanel1247 = setInterval(rereshRetry2DemoPanel1247,1000); var binded1247 = false; $("textarea#stepcodeTextarea1247").keyup(function(){ if(!binded1247){ $(window).bind('beforeunload',function(){ binded1247 = true; return "xxxx"; }); } var newCode = $(this).val() code1247 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1247!=newCode){ // window.frames["iframe1247"].document.write("
"); // window.frames["iframe1247"].document.write(decodeHtml($("textarea#stepcodeTextarea1247").val())); // window.frames["iframe1247"].document.close(); // $(window.frames["iframe1247"]).load(function(){ // $("#iframe1247").height($("#iframe1247").contents().find("body").height()+showittryitheight); // }); // code1247 = newCode; // } }); $(".tryButton1247").click(function(){ $("#tryDiv1247").show(); $("#stepcodeTextarea1247").focus(); $("#stepcodeTextarea1247").height(200); $("#iframe1247").height(0); window.frames["iframe1247"].document.write("
"); window.frames["iframe1247"].document.write(decodeHtml($("textarea#stepcodeTextarea1247").val())); window.frames["iframe1247"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1247"]).load(function(){ $("#iframe1247").height($("#iframe1247").contents().find("body").height()+showittryitheight); }); $("#iframe1247").height($("#iframe1247").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1247.focus(); editor1247.setSize(null, "250"); $("#rendering1247").hide(); $("#rendered1247").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1247 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1247"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1247.on("change",function(doc){ if(!binded1247){ $(window).bind('beforeunload',function(){ binded1247 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1247 = newCode; $("textarea#stepcodeTextarea1247").val(newCode); if(alreadyWriteCode1247!=code1247){ lastModifedTime1247 = new Date().getTime(); $("#rendering1247").show(); $("#rendered1247").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1247 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1247.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1247").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 2 :

父 祖先

parent() 选取最近的一个父元素
parents() 选取所有的祖先元素
"); window.frames["iframe_show1248"].document.write(decodeHtml($("textarea#stepcodeTextarea1248").val())); window.frames["iframe_show1248"].document.close(); $(window.frames["iframe_show1248"]).load(function(){ $("#iframe_show1248").height($("#iframe_show1248").contents().find("body").height()+showittryitheight); }); $("#iframe_show1248").height($("#iframe_show1248").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/jquery.min.js"></script> <script> $(function(){ $("#b1").click(function(){ $("#currentDiv").parent().toggleClass("b"); }); $("#b2").click(function(){ $("#currentDiv").parents().toggleClass("b"); }); }); </script> <style> div{ padding:20px; } div#grandParentDiv{ background-color:pink; } div#parentDiv{ background-color:green; } div#currentDiv{ background-color:red; } .b{ border: 2px solid black; } </style> <button id="b1">改变parent()的边框</button> <button id="b2">改变parents()的边框</button> <div id="grandParentDiv" > 祖先元素 <div id="parentDiv"> 父元素 <div id="currentDiv">当前元素</div> </div> </div>
"); window.frames["iframe1248"].document.write(decodeHtml(code1248)); window.frames["iframe1248"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1248"]).load(function(){ $("#iframe1248").height($("#iframe1248").contents().find("body").height()+showittryitheight); }); $("#iframe1248").height($("#iframe1248").contents().find("body").height()+showittryitheight); alreadyWriteCode1248 = code1248; $("#rendering1248").hide(); $("#rendered1248").show(); } var tRereshRetry2DemoPanel1248 = setInterval(rereshRetry2DemoPanel1248,1000); var binded1248 = false; $("textarea#stepcodeTextarea1248").keyup(function(){ if(!binded1248){ $(window).bind('beforeunload',function(){ binded1248 = true; return "xxxx"; }); } var newCode = $(this).val() code1248 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1248!=newCode){ // window.frames["iframe1248"].document.write("
"); // window.frames["iframe1248"].document.write(decodeHtml($("textarea#stepcodeTextarea1248").val())); // window.frames["iframe1248"].document.close(); // $(window.frames["iframe1248"]).load(function(){ // $("#iframe1248").height($("#iframe1248").contents().find("body").height()+showittryitheight); // }); // code1248 = newCode; // } }); $(".tryButton1248").click(function(){ $("#tryDiv1248").show(); $("#stepcodeTextarea1248").focus(); $("#stepcodeTextarea1248").height(200); $("#iframe1248").height(0); window.frames["iframe1248"].document.write("
"); window.frames["iframe1248"].document.write(decodeHtml($("textarea#stepcodeTextarea1248").val())); window.frames["iframe1248"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1248"]).load(function(){ $("#iframe1248").height($("#iframe1248").contents().find("body").height()+showittryitheight); }); $("#iframe1248").height($("#iframe1248").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1248.focus(); editor1248.setSize(null, "250"); $("#rendering1248").hide(); $("#rendered1248").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1248 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1248"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1248.on("change",function(doc){ if(!binded1248){ $(window).bind('beforeunload',function(){ binded1248 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1248 = newCode; $("textarea#stepcodeTextarea1248").val(newCode); if(alreadyWriteCode1248!=code1248){ lastModifedTime1248 = new Date().getTime(); $("#rendering1248").show(); $("#rendered1248").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1248 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1248.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1248").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 3 :

儿子 后代

children(): 筛选出儿子元素 (紧挨着的子元素)
find(selector): 筛选出后代元素
注: find() 必须使用参数 selector
"); window.frames["iframe_show1249"].document.write(decodeHtml($("textarea#stepcodeTextarea1249").val())); window.frames["iframe_show1249"].document.close(); $(window.frames["iframe_show1249"]).load(function(){ $("#iframe_show1249").height($("#iframe_show1249").contents().find("body").height()+showittryitheight); }); $("#iframe_show1249").height($("#iframe_show1249").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/jquery.min.js"></script> <script> $(function(){ $("#b1").click(function(){ $("#currentDiv").children().toggleClass("b"); }); $("#b2").click(function(){ $("#currentDiv").find("div").toggleClass("b"); }); }); </script> <style> div{ padding:20px; } div.grandChildrenDiv{ background-color:pink; } div.childrenDiv{ background-color:green; margin:10px; } div#currentDiv{ background-color:red; } .b{ border: 2px solid black; } </style> <button id="b1">改变children()的边框</button> <button id="b2">改变find()的边框</button> <div id="currentDiv" > 当前元素 <div class="childrenDiv"> 儿子元素1 <div class="grandChildrenDiv">后代元素n</div> </div> <div class="childrenDiv"> 儿子元素2 <div class="grandChildrenDiv">后代元素n</div> </div> </div>
"); window.frames["iframe1249"].document.write(decodeHtml(code1249)); window.frames["iframe1249"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1249"]).load(function(){ $("#iframe1249").height($("#iframe1249").contents().find("body").height()+showittryitheight); }); $("#iframe1249").height($("#iframe1249").contents().find("body").height()+showittryitheight); alreadyWriteCode1249 = code1249; $("#rendering1249").hide(); $("#rendered1249").show(); } var tRereshRetry2DemoPanel1249 = setInterval(rereshRetry2DemoPanel1249,1000); var binded1249 = false; $("textarea#stepcodeTextarea1249").keyup(function(){ if(!binded1249){ $(window).bind('beforeunload',function(){ binded1249 = true; return "xxxx"; }); } var newCode = $(this).val() code1249 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1249!=newCode){ // window.frames["iframe1249"].document.write("
"); // window.frames["iframe1249"].document.write(decodeHtml($("textarea#stepcodeTextarea1249").val())); // window.frames["iframe1249"].document.close(); // $(window.frames["iframe1249"]).load(function(){ // $("#iframe1249").height($("#iframe1249").contents().find("body").height()+showittryitheight); // }); // code1249 = newCode; // } }); $(".tryButton1249").click(function(){ $("#tryDiv1249").show(); $("#stepcodeTextarea1249").focus(); $("#stepcodeTextarea1249").height(200); $("#iframe1249").height(0); window.frames["iframe1249"].document.write("
"); window.frames["iframe1249"].document.write(decodeHtml($("textarea#stepcodeTextarea1249").val())); window.frames["iframe1249"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1249"]).load(function(){ $("#iframe1249").height($("#iframe1249").contents().find("body").height()+showittryitheight); }); $("#iframe1249").height($("#iframe1249").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1249.focus(); editor1249.setSize(null, "250"); $("#rendering1249").hide(); $("#rendered1249").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1249 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1249"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1249.on("change",function(doc){ if(!binded1249){ $(window).bind('beforeunload',function(){ binded1249 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1249 = newCode; $("textarea#stepcodeTextarea1249").val(newCode); if(alreadyWriteCode1249!=code1249){ lastModifedTime1249 = new Date().getTime(); $("#rendering1249").show(); $("#rendered1249").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1249 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1249.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1249").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 4 :

同级

siblings(): 同级(同胞)元素
"); window.frames["iframe_show1250"].document.write(decodeHtml($("textarea#stepcodeTextarea1250").val())); window.frames["iframe_show1250"].document.close(); $(window.frames["iframe_show1250"]).load(function(){ $("#iframe_show1250").height($("#iframe_show1250").contents().find("body").height()+showittryitheight); }); $("#iframe_show1250").height($("#iframe_show1250").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/jquery.min.js"></script> <script> $(function(){ $("#b1").click(function(){ $("#currentDiv").siblings().toggleClass("b"); }); }); </script> <style> div{ padding:20px; background-color:pink; margin:10px; } div#parentDiv{ background-color:green; } div#currentDiv{ background-color:red; } .b{ border: 2px solid black; } </style> <button id="b1">给同级加上边框</button> <div id="parentDiv" > 父元素 <div id="currentDiv"> 当前元素 </div> <div > 同级元素 </div> <div > 同级元素 </div> </div>
"); window.frames["iframe1250"].document.write(decodeHtml(code1250)); window.frames["iframe1250"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1250"]).load(function(){ $("#iframe1250").height($("#iframe1250").contents().find("body").height()+showittryitheight); }); $("#iframe1250").height($("#iframe1250").contents().find("body").height()+showittryitheight); alreadyWriteCode1250 = code1250; $("#rendering1250").hide(); $("#rendered1250").show(); } var tRereshRetry2DemoPanel1250 = setInterval(rereshRetry2DemoPanel1250,1000); var binded1250 = false; $("textarea#stepcodeTextarea1250").keyup(function(){ if(!binded1250){ $(window).bind('beforeunload',function(){ binded1250 = true; return "xxxx"; }); } var newCode = $(this).val() code1250 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1250!=newCode){ // window.frames["iframe1250"].document.write("
"); // window.frames["iframe1250"].document.write(decodeHtml($("textarea#stepcodeTextarea1250").val())); // window.frames["iframe1250"].document.close(); // $(window.frames["iframe1250"]).load(function(){ // $("#iframe1250").height($("#iframe1250").contents().find("body").height()+showittryitheight); // }); // code1250 = newCode; // } }); $(".tryButton1250").click(function(){ $("#tryDiv1250").show(); $("#stepcodeTextarea1250").focus(); $("#stepcodeTextarea1250").height(200); $("#iframe1250").height(0); window.frames["iframe1250"].document.write("
"); window.frames["iframe1250"].document.write(decodeHtml($("textarea#stepcodeTextarea1250").val())); window.frames["iframe1250"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1250"]).load(function(){ $("#iframe1250").height($("#iframe1250").contents().find("body").height()+showittryitheight); }); $("#iframe1250").height($("#iframe1250").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1250.focus(); editor1250.setSize(null, "250"); $("#rendering1250").hide(); $("#rendered1250").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1250 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1250"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1250.on("change",function(doc){ if(!binded1250){ $(window).bind('beforeunload',function(){ binded1250 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1250 = newCode; $("textarea#stepcodeTextarea1250").val(newCode); if(alreadyWriteCode1250!=code1250){ lastModifedTime1250 = new Date().getTime(); $("#rendering1250").show(); $("#rendered1250").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1250 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1250.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1250").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 前端部分-JQuery-筛选器 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 620943819
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: http://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图