01、基本选择器
标签选择器 $(“a”)
ID选择器 $(“#id”) $(“p#id”)
类选择器 $(“.class”) $(“h2.class”)
通配选择器 $("*")
并集选择器$(“elem1,elem2,elem3”)
<script type="text/javascript">$(function(){/*******id选择器***********///Elementvaruname=document.getElementById("uname");// Object [Element1,Element2,Element3]// JQ方式获得的元素var uname2=$("#uname"); /******元素选择器**************/ //js 获得所有的input对象varinp=document.getElementsByTagName("input");//JQvar inp2=$("input");
// JQ对象转化成JS对象 [0] object ---Element
// alert(inp2[0].value);//操作的完全是JQ对象 alert(inp2.eq(0).val()); /********类选择器*********************/var inp3=$(".inp");//所有选择器 $("*").css("background-color","green");})</script></head><body><p>姓名: <input type="text" name="uname" id="uname" value="123" class="inp"/> </p> <p>密码: <input type="password" name="pwd" id="pwd" value="sxt" class="inp" /></p></body>
02、层级选择器
后代选择器$(ul li)
父子选择器 $(ul>li)
后面第一个兄弟元素(紧跟) prev + next
后面所有的兄弟元素(同辈) prev ~ next
<script type="text/javascript">$(function(){ //包含选择器 √
// $("#div1 span").css("backgroundColor","red");//子父选择器 div1下面的直系子节点 √
// $("#div1>span").css("backgroundColor","red");//后面紧跟的下一个元素
// $("#span1 + span").css("backgroundColor","red");//获得指定元素后面所有的span标签 ---同辈 √$("#span1 ~ span").css("backgroundColor","red");}) </script></head><body><div id="div1" style="height: 400px; border: 3px solid red;"><span id="span1"> List Item2 </span> <br /><span> List Item3 </span> <br /><span> List Item4 </span><br /><p><span> List Item5 </span><br /><span> List Item6 </span><br /><span> List Item7 </span><br /></p></div>
03、位置选择器
:first :last :odd :even :eq(index) :lt(index) :gt(index)
<script type="text/javascript">$(function(){/**********位置选择器*********************///获取第一个元素 √$("ul li").first().css("backgroundColor","red");$("ul li:first").css("backgroundColor","red");//获得最后一个元素 √$("ul li").last().css("backgroundColor","red");$("ul li:last").css("backgroundColor","red");//获得所有的奇数行对象 √ $("ul li:even").css("backgroundColor","red");//获得所有的偶数行对象 √$("ul li:odd").css("backgroundColor","red");//获得索引下标的对象 从0开始 √$("ul li:eq(4)").css("backgroundColor","red");$("ul li").eq(3).css("backgroundColor","red");//获得角标大于3的元素对象$("ul li:gt(3)").css("backgroundColor","red");//获得角标小于3的元素对象$("ul li:lt(3)").css("backgroundColor","red"); })</script></head><body><div id="div1" style="height: 300px; border: 3px solid red;"><ul><li>List Item1 </li><li>List Item2 </li><li>List Item3 </li><li>List Item4 </li></ul> <ul><li>List2 Item1 </li> <!-- <li>List2 Item2 </li>--> </ul></div>
04、子元素选择器
:nth-child(index) 这个是从1开始的
:only-child
:first-child
:last-child
$(“input[name=bo]”)
<script type="text/javascript">$(function(){/**************子元素选择器***********************///匹配其父元素下的第N个子或奇偶元素 从1开始 √$("ul li:nth-child(1)").css("backgroundColor","red"); $("ul li:nth-child(even)").css("backgroundColor","red");//匹配第一个子元素 $("ul li:first-child").css("backgroundColor","red"); //匹配最后一个子元素$("ul li:last-child").css("backgroundColor","red"); //如果某个元素是父元素中唯一的子元素,那将会被匹配$("ul li:only-child").css("backgroundColor","red");})</script></head><body><div id="div1" style="height: 300px; border: 3px solid red;"><ul><li>List Item1 </li><li>List Item2 </li><li>List Item3 </li><li>List Item4 </li></ul><ul><li>List2 Item1 </li><!-- <li>List2 Item2 </li>--></ul></div>
05、属性选择器
[attribute] [attribute1][attribute2] [attribute=value] [attribute!=value]
[attribute^=value] [attribute$=value] [attribute*=value]
<script type="text/javascript">$(function(){/*********属性选择器********************///属性等于指定内容的元素对象 √$("input[type='text']").css("backgroundColor","red");//获得属性用s开头额元素对象$("input[name^='s']").css("backgroundColor","red");//匹配给定的属性是以某些值结尾的元素$("input[name$='d']").css("backgroundColor","red");//属性中包括指定元素的对象//$("input[name*='man']") })</script></head><body><center><h3>注册用户</h3><form action="doRegister.jsp" method="get"><table border="1" width="40%"> <tr><td>用户名</td><td><input type="text" name="username" id="username" value="请输入姓名" disabled="disabled"/></td></tr><tr><td>密 码</td><td><input type="password" name="pwd" id="pwd" /></td></tr><tr><td>确认密码</td><td><input type="color" name="spwd" id="repwd"/></td></tr><tr><td>性别</td><td><input type="radio" name="sex" value="男"/>男<input type="radio" name="sex" checked="checked" value="女"/>女</td></tr><tr><td>年龄</td><td><input type="number" name="sage" id="age" value="18"/></td></tr><tr><td>电子邮箱</td><td><input type="email" name="semail" id="email"/></td></tr><tr><td>爱好</td><td><input type="checkbox" name="hobby" value="music" checked/>音乐<input type="checkbox" name="hobby" value="sports" />体育<input type="checkbox" name="hobby" value="art" />美术</td></tr><tr><td>身份</td><td><select name="professional"><option value="1">工人</option><option value="2">农民</option><option value="3" selected="selected">解放军</option><option value="4">知识分子</option></select></td></tr><tr><td>简历</td><td><textarea name="resume" rows="5" cols="40">请输入简历</textarea></td></tr><tr><td>照片</td><td><input type="file" name="photo" id="photo"/></td></tr><tr><td colspan="2" align="center"><input type="submit" value="提交" disabled="disabled"/><input type="reset" value="重置"/><input type="button" value="返回" onclick="alert('back')"/></td></tr></table></form></center>
06、表单选择器
:text :password :radio :checkbox :hidden :file :submit
:input 匹配所有 input, textarea, select 和 button 元素
:selected :checked :enabled :disabled
:hidden :visible :not
注意$(“input”)
和$(“:input”)
的区别
<script type="text/javascript">$(function(){/********表单选择器(只是用于表单的对象)*******/$("input[type='text']").css("background-color","red");√$(":text").css("background-color","red");$(":password").css("background-color","red");//获得所有的表单项---16 √alert($(":input").length);//获得所有的input对象--14alert($("input").length);/**********表单属性选择器**************///获得多选框中的被选择的对象 √$("input[name='hobby']:checked");//获得input中name属性是uname的并且含有disabled属性的对象 √$("input[name='uname']:disabled")//匹配所有选中的option元素 √$("select option:selected"); })</script></head><body><center><h3>注册用户</h3><form action="doRegister.jsp" method="get"><table border="1" width="40%"> <tr><td>用户名</td><td><input type="text" name="username" id="username" value="请输入姓名" disabled="disabled"/></td></tr><tr><td>密 码</td><td><input type="password" name="pwd" id="pwd" /></td></tr><tr><td>确认密码</td><td><input type="color" name="spwd" id="repwd"/></td></tr><tr><td>性别</td><td><input type="radio" name="sex" value="男"/>男<input type="radio" name="sex" checked="checked" value="女"/>女</td></tr><tr><td>年龄</td><td><input type="number" name="sage" id="age" value="18"/></td></tr><tr><td>电子邮箱</td><td><input type="email" name="semail" id="email"/></td></tr><tr><td>爱好</td><td><input type="checkbox" name="hobby" value="music" checked/>音乐<input type="checkbox" name="hobby" value="sports" />体育<input type="checkbox" name="hobby" value="art" />美术</td></tr><tr><td>身份</td><td><select name="professional"><option value="1">工人</option><option value="2">农民</option><option value="3" selected="selected">解放军</option><option value="4">知识分子</option></select></td></tr><tr><td>简历</td><td><textarea name="resume" rows="5" cols="40">请输入简历</textarea></td></tr><tr><td>照片</td><td><input type="file" name="photo" id="photo"/></td></tr><tr><td colspan="2" align="center"><input type="submit" value="提交" disabled="disabled"/><input type="reset" value="重置"/><input type="button" value="返回" onclick="alert('back')"/></td></tr></table></form></center>