经过一晚上的查找整理,终于整理出一套应该算最全面的JQuery选择过滤器的方法了。所有代码均经过测试。
首先HTML代码
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
< html >
< head >
< script type ="text/javascript" src ="JQuery/jquery-1.5.1.js" ></ script >
</ head >
< body >
< form name ="form1" id ="form1" class ="form1" action ="" method ="post" >
< table name ="table1" id ="table1" class ="table1" >
< tr name ="tr1" id ="tr1" class ="tr1" >
< td name ="td11" id ="td11" class ="td11" nowrap >
< input type ="text" name ="text11" id ="text11" class ="text11" value ="text11" />
< input type ="button" name ="button11" id ="button11" class ="button11" value ="button11" />
</ td >
< td name ="td12" id ="td12" class ="td12" >
< input type ="text" name ="text12" id ="text12" class ="text12" value ="text12" />
< input type ="hidden" name ="hidden12" id ="hidden12" class ="hidden12" value ="hidden12" />
</ td >
< td name ="td13" id ="td13" class ="td13" >
< input type ="text" name ="text13" id ="text13" class ="text13" value ="text13" />
</ td >
</ tr >
< tr name ="tr2" id ="tr2" class ="tr2" >
< td name ="td21" id ="td21" class ="td21" >
< input type ="password" name ="password21" id ="password21" class ="password21" value ="password21" />
< input type ="radio" name ="radio21" id ="radio21" class ="radio21" value ="radio21" checked />
< input type ="checkbox" name ="checkbox21" id ="checkbox21" class ="checkbox21" value ="checkbox21" checked />
</ td >
< td name ="td22" id ="td22" class ="td22" nowrap >
< input type ="submit" name ="submit22" id ="submit22" class ="submit22" value ="submit22" />
< input type ="image" name ="image22" id ="image22" class ="image22" value ="image22" />
< input type ="reset" name ="reset22" id ="reset22" class ="reset22" value ="reset22" />
</ td >
< td name ="td23" id ="td23" class ="td23" >
< input type ="file" name ="file23" id ="file23" class ="file23" value ="file23" />
</ td >
</ tr >
< tr name ="tr3" id ="tr3" class ="tr3" >
< td name ="td31" id ="td31" class ="td31" >
< select name ="select31" size ="1" >
< option value ="select311" > option1 </ option >
< option value ="select312" selected > option2 </ option >
< option value ="select313" > option3 </ option >
</ select >
</ td >
< td name ="td32" id ="td32" class ="td32" ></ td >
< td name ="td33" id ="td33" class ="td33" ></ td >
</ tr >
</ table >
</ form >
< h1 > 你好世界! </ h1 >
</ body >
</ html >
JS代码
1.DOM对象与JQuery包装集
1.通过document.getElementById(),document.getElementsByName()等取的就是DOM对象或DOM对象集,前者取的是对象,后者取得DOM对象集
var text11_dom = document.getElementById( " text11 " );
var text11_dom = document.getElementsByName( " text11 " )[ 0 ];
var text11_dom = document.all.text11; // 此处text11即可以是name值也可以是id值
var text11_dom = document.all[ 10 ];
var text11_jquery = $( " #text11 " );
2.JQuery包装集与DOM对象互相转换
1.DOM对象不能使用JQuery属性方法,但DOM对象可以通过$()转换成JQuery包装集
var text11_dom = document.getElementById( " text11 " );
var text11_jquery = $(text11_dom);
var text11_dom = $( " #text11 " )[ 0 ];
$( " #text11 " ).click( function (){
var text11_dom_value = this .value;
alert(text11_dom_value);
});
3.$符号在JQuery中代表对JQuery对象的引用,JQuery的核心方法有四个
1.jQuery(html[,ownerDocument]):根据HTML原始字符串动态创建Dom元素
$( " <div><p>Hello!</p></div> " ).appendTo( " body " );
3.jQuery( callback ):$(document).ready()的简写方式
$( function (){
alert( " Hello! " );
});
在所有tr标签中查找id为text11元素的JQuery包装集
var text11_query = $( " #text11 " , " tr " );
===================jQuery选择器===================
1. 基础选择器 Basics
1.根据标签名进行选择
var input_query = $( " input " );
var text11_query = $( " #text11 " );
var text11_query = $( " .text11 " );
var text_query = $( " #text11,.text12 " );
var all_query = $( " * " );
2.层次选择器 Hierarchy
1.从所有tr标签中获取其下面的所有id值为text11的元素
var text11_query = $( " tr #text11 " );
var input_query = $( " td>input " );
var button11_query = $( " #text11+.button11 " );
var button11_query = $( " #text11~.button11 " );
3.基本过滤器 Basic Filters
1.获取第一个input元素
var input_query = $( " input:first " );
var input_query = $( " input:last " );
var input_query = $( " input:not(:checked) " );
var input_query = $( " input:even " );
var input_query = $( " input:odd " );
var input_query = $( " input:eq(1) " );
var input_query = $( " input:gt(0) " );
var input_query = $( " input:lt(2) " );
var h_query = $( " :header " );
var animated_query = $( " :animated " );
4. 内容过滤器 Content Filters
1.查找所有html内容含有"你好世界!"的h1元素
var h1_query = $( " h1:contains('你好世界!') " );
var td_query = $( " td:empty " );
var td_query = $( " td:has(input) " );
var td_query = $( " td:parent " );
5.可见性过滤器 Visibility Filters
1.查找所有隐藏的input元素
var input_query = $( " input:hidden " );
var input_query = $( " input:visible " );
6.属性过滤器 Attribute Filters
1.查找所有含有id属性的input元素
var input_query = $( " input[id] " );
var input_query = $( " input[name='text11'] " );
var input_query = $( " input[name!='text11'] " );
var input_query = $( " input[name^='text'] " );
var input_query = $( " input[name$='11'] " );
var input_query = $( " input[name*='ext'] " );
var input_query = $( " input[id][name*='ext'] " );
7.子元素过滤器 Child Filters
1.查找所有在父元素中的所有子元素中排第2的input元素
nth-child()里参数可选even在这里算偶数,odd在这里算奇数,n任意数即选取所有有父元素的input元素,数字是直接选排在第几个的input元素,第一个input元素算一
var input_query = $( " input:nth-child(2) " );
var input_query = $( " input:first-child " );
var input_query = $( " input:last-child " );
var input_query = $( " input:only-child " );
8.表单选择器 Forms
1.查找所有input元素
var input_query = $( " :input " );
var text_query = $( " :text " );
var password_query = $( " :password " );
var checkbox_query = $( " :checkbox " );
var submit_query = $( " :submit");
var image_query = $( " :image " );
var reset_query = $( " :reset " );
var button_query = $( " :button " );
var file_query = $( " :file " );
9.表单过滤器 Form Filters
1.查找所有可用的input元素
var input_query = $( " input:enabled " );
var input_query = $( " input:disabled " );
var input_query = $( " input:checked " );
var option_query = $( " option:selected " );