$("#myDiv"); //根据给定的ID匹配一个元素,用于搜索id 属性中给定的值,id属性必须是唯一的
$("div"); //根据给定的元素名匹配所有元素,搜索指向dom节点的标签名
$(".myClass"); //根据给定的类匹配元素,用以搜索的类。一个元素可以有多个类,只要有一个符合就能被匹配到
$("div,.myClass"); //将每一个选择器匹配到的元素合并后一起返回,可以指定多个元素
返回示例:
<Div>This is Div</Div><Pclass= ". MyClass">Specifying classes</P>Results:<Div>Div</Div>,<Pclass= ". MyClass"></P>
$("form input");或者
$("div input"); //在给定的父元素元素下匹配所有的子元素
$("div > input"); //给定的父元素下匹配所有的子元素
<Div> <label>Id</label> <inputname= "Name" /> <ul> <Li>Newsletter:</Li> </ul></Div><inputname= "None" />Results:<inputname= "Name" />
$(‘li:first‘)
; Get the first element under Li
$(‘li:last‘); //获取li下面的最后一个元素
$("input:not(:checked)")或者$("div:not(a)");
//排除掉input所有选中的元素筛选,排除掉div下的所有a标签
$("li:even"); //匹配所有索引值为偶数的元素,从 0 开始计数
例:
<ul> <Li>First one</Li> <Li>A second</Li> <Li>A third</Li> <Li>Fourth one</Li></ul>Results:<Li>First one</Li>,<Li>A third</Li>
$("li:odd"); //匹配所有索引值为奇数的元素,从 0 开始计数,与上面例子相反
$("li:eq(1)"); //匹配给定索引值的元素,从0开始计数,拿上面例子,返回结果为<li>第二个</li>
$("li:gt(0)")和
$("li:lt(2)"); //第一个查找所有大于给定索引值的元素,第二个所有小于给定索引值的元素
例:
<ul> <Li>First one</Li> <Li>A second</Li> <Li>A third</Li> <Li>Fourth one</Li></ul>First return result: all elements greater than the given index value:<Li>A second</Li>,<Li>A third</Li>,<Li>Fourth one</Li>second return result:All elements smaller than the given index value:<Li>First one</Li>,<Li>A second</Li>,<Li>A third</Li>
$(".myClass").css("background", "#EEE"); //给类名myClass的所有元素加背景颜色
$("#myId:focus"); //给id名为myId设置焦点
$("div:contains(‘sevennight‘)"); //查找所有div下包含sevennight的元素
$("div:has(p)").addClass("test");给所有
包含 p 元素的 div 元素添加一个 text 类
$("td:parent"); //查找所有含有子元素或者文本的 td 元素
$("div:hidden"); //查找隐藏的 div,或者type为hidden的元素
$("div:visible"); //查找所有的可见div元素
$("input[name=‘myOption‘]").attr("checked", true); //查找input所有input标签name等于myOption的属性
checked为true的元素
$("input[name!=‘myOption‘]").attr("checked", true); //查找input所有input标签name不等于myOption的属性
checked为true的元素
$("input[name^=‘seven‘]"); //查找input name以seven开始的元素,
<input name="sevennight" />, <input name="sevenDay" />
$("input[name$=‘day‘]"); //查找input name以day结尾的元素,与上面相反
$("input[name*=‘a‘]"); //查找所有input name包含a的元素
$("input[‘.myClass‘][name$=‘over‘]"); //查找class名为myClass的元素,并且此元素name以over结尾
$("input:checked"); //查找所有选中的复选框,单选框
$("select option:selected"); //查找option所有选中的选项元素