ID选择器
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery选择器之id选择器title>
<style>
div {
width:100px;
height:90px;
float:left;
padding:5px;
margin:10px;
background-color: #838685;
}
style>
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js">script>
head>
<body>
<div id="js">
<p>div id="js"p>
<p>got you!p>
div>
<div id="jq">
<p>div id="jq"p>
<p>got you!p>
div>
<div id="no">
<p>div id="no"p>
<p>got you!p>
div>
<script type="text/javascript">
var js = document.getElementById('js');
js.style.border = '3px solid blue';
script>
<script type="text/javascript">
$('#jq').css('border','3px solid red');
script>
body>
html>
类选择器
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery类选择器title>
<style>
div {
width: 100px;
height: 100px;
float: left;
padding: 5px;
margin: 10px;
background-color: #cccccc;
}
style>
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js">script>
head>
<body>
<div class="js">
<p>div id="js"p>
<p>got you!p>
div>
<div class ="jq">
<p>div id="jq"p>
<p>got you!p>
div>
<div id="no">
<p>div id="no"p>
<p>got you!p>
div>
<script type="text/javascript">
var oDiv = document.getElementsByClassName('js');
for(var i=0; i'3px solid green';
}
script>
<script type="text/javascript">
$('.jq').css('border','3px solid red');
script>
body>
html>
元素选择器
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>title>
<style>
div {
width: 100px;
height: 90px;
float: left;
padding: 5px;
margin: 5px;
background-color: #EEEEEE;
}
style>
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js">script>
head>
<body>
<div class="aaron">
<p>class="aaron"p>
<p>选中p>
div>
<div class="aaron">
<p>class="aaron"p>
<p>选中p>
div>
<div class="imooc">
<p>class="imooc"p>
<p>jQuery选中p>
div>
<div class="imooc">
<p>class="imooc"p>
<p>jQuery选中p>
div>
<script type="text/javascript">
var divs = document.getElementsByTagName('div');
for (var i = 0; i < divs.length; i++) {
divs[i].style.border = "5px solid green";
}
script>
<script type="text/javascript">
$('p').css("border", "5px solid red");
script>
body>
html>
全选择器
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>jQuery全选择器title>
<style>
div {
width: 100px;
height: 90px;
float: left;
padding: 5px;
margin: 5px;
background-color: #EEEEEE;
}
style>
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js">script>
head>
<body>
<div class="aaron">
<p>class="aaron"p>
<p>选中p>
div>
<div class="aaron">
<p>class="aaron"p>
<p>选中p>
div>
<div class="imooc">
<p>class="imooc"p>
<p>jQuery选中p>
div>
<div class="imooc">
<p>class="imooc"p>
<p>jQuery选中p>
div>
<script type="text/javascript">
var elements1 = document.getElementsByTagName('*');
script>
<script type="text/javascript">
var elements2 = $('*');
if(elements2.length === elements1.length){
elements2.css("border","3px solid red");
}
script>
body>
html>
层级选择器
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>jQuery层级选择器title>
<link rel="stylesheet" href="imooc.css" type="text/css">
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js">script>
head>
<body>
<h2>子选择器与后代选择器h2>
<div class="left">
<div class="aaron">
<p>div下的第一个p元素p>
div>
<div class="aaron">
<p>div下的第一个p元素p>
div>
div>
<div class="right">
<div class="imooc">
<article>
<p>div下的article下的p元素p>
article>
div>
<div class="imooc">
<article>
<p>div下的article下的p元素p>
article>
div>
div>
<script type="text/javascript">
$('div>p').css("border", "5px solid green");
script>
<script type="text/javascript">
$('div article').css("border", "5px solid red");
script>
<h2>相邻兄弟选择器与一般兄弟选择器h2>
<div class="bottom">
<div>兄弟节点div, +~选择器不能向前选择div>
<span class="prev">选择器span元素span>
<div>span后第一个兄弟节点divdiv>
<div>兄弟节点div
<div class="small">子元素divdiv>
div>
<span>兄弟节点span,不可选span>
<div>兄弟节点divdiv>
div>
<script type="text/javascript">
$('.prev+div').css("border", "3px solid blue");
script>
<script type="text/javascript">
$('.prev~span').css("border", "3px solid red");
script>
body>
html>
基本筛选选择器
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>jQuery基本筛选选择器title>
<link rel="stylesheet" href="imooc.css" type="text/css">
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js">script>
head>
<body>
<h2>基本筛选器h2>
<h3>:first/:last/:even/:oddh3>
<div class="left">
<div class="div">
<p>div:firstp>
<p>:evenp>
div>
<div class="div">
<p>:oddp>
div>
<div class="div">
<p>:evenp>
div>
很多时候我们不能直接通过基本选择器与层级选择器找到我们想要的元素,
为此jQuery提供了一系列的筛选选择器用来更快捷的找到所需的DOM元素。
筛选选择器很多都不是CSS的规范,而是jQuery自己为了开发者的便利延展出来的选择器
<div class="div">
<p>:oddp>
div>
<div class="div">
<p>:evenp>
div>
<div class="div">
<p>div:lastp>
<p>:oddp>
div>
div>
<script type="text/javascript">
$('.div:first').css("color", "#CD00CD");
script>
<script type="text/javascript">
$('.div:last').css("color", "#CD00CD");
script>
<script type="text/javascript">
$('.div:even').css("border", "3px groove red");
script>
<script type="text/javascript">
$('.div:odd').css("border", "3px groove blue");
script>
<h3>:eq/:gt/:lth3>
<div class="left">
<div class="choose">
<p>:lt(3)p>
div>
<div class="choose">
<p>:lt(3)p>
div>
<div class="choose">
<p>:eq(2)p>
div>
<div class="choose">
div>
<div class="choose">
<p>:gt(3)p>
div>
<div class="choose">
<p>:gt(3)p>
div>
div>
<script type="text/javascript">
$('.choose:eq(2)').css("border", "3px groove blue");
script>
<script type="text/javascript">
$('.choose:gt(3)').css("border", "3px groove blue");
script>
<script type="text/javascript">
$('.choose:lt(2)').css("color", "#CD00CD");
script>
<h3>:noth3>
<div class="left">
<div>
<input type="checkbox" name="a" />
<p>choosep>
div>
<div>
<input type="checkbox" name="b" />
<p>worldp>
div>
<div>
<input type="checkbox" name="c" checked="checked" />
<p>othersp>
div>
div>
<script type="text/javascript">
$('input:not(:checked)+p').css("background-color", "#CD00CD");
script>
body>
html>
内容筛选选择器
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>jQuery内容筛选选择器title>
<link rel="stylesheet" href="imooc.css" type="text/css">
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js">script>
head>
<body>
<h3>:contains/:hash3>
<div class="left">
<div class="div">
<p>contentp>
div>
<div class="div">
<p>contentp>
div>
<div class="div">
<p>
<span>:hasspan>
p>
div>
<div class="div">
<p>contentp>
div>
div>
<script type="text/javascript">
$('.div:contains("content")').css("color", "#CD00CD");
script>
<script type="text/javascript">
$('.div:has(span)').css("color", "blue");
script>
<h3>:parent/:emptyh3>
<div class="left">
<div class="aaron">
<a>:parenta>
div>
<div class="aaron">
<a>:parenta>
div>
<div class="aaron">
<a>:parenta>
div>
<div class="aaron">
<a>a>
div>
div>
<script type="text/javascript">
$('a:parent').css("border", "3px groove blue");
script>
<script type="text/javascript">
$('a:empty').text(":empty").css("border", "3px groove red");
script>
body>
html>
可见性筛选选择器
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery可见性筛选选择器title>
<link rel="stylesheet" href="2-9.css" type="text/css">
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js">script>
head>
<body>
隐藏一个元素的方式:
1,CSS display属性的值为none。
2,type="hidden"的表单元素。
3,宽度和高度都显式设置为0。
4,一个祖先元素是隐藏的,该元素是不会在页面上显示的。
5,CSS visibility的值是hidden。
6,CSS opacity的值是0。
<h2>可见性筛选选择器h2>
<h3>:visible/:hiddenh3>
<div class="left">
<div class="div">
<a>displaya>
<p id="div1" style="display:none;">displayp>
div>
<div class="div">
<a>widtha>
<a>heighta>
<p id="div2" style="width:0;height:0">width/heightp>
div>
<div class="div">
<a>visibilitya>
<a>opacitya>
<p id="div3" style="visibility:hidden;opacity:0">visibilityp>
div>
<p id="show">p>
<script type="text/javascript">
function show(ele){
if(ele instanceof jQuery){
$('#show').html('元素的长度:'+ ele.length)
}else{
alert(ele+' 不是jQuery对象')
}
}
script>
<script type="text/javascript">
show($('#div1:visible'));
script>
<script type="text/javascript">
show($('#div2:visible'));
script>
<script type="text/javascript">
show($('#div3:visible'));
script>
<script type="text/javascript">
show($('#div1:hidden'));
script>
<script type="text/javascript">
show($('#div2:hidden'));
script>
<script type="text/javascript">
show($('#div3:hidden'));
script>
div>
body>
html>
属性筛选选择器
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>jQuery子元素筛选选择器title>
<link rel="stylesheet" href="2-9.css" type="text/css">
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js">script>
head>
<body>
<h2>属性筛选选择器h2>
<h3>[att=val]、[att]、[att|=val]、[att~=val]h3>
<div class="left" testattr="true" >
<div class="div" testattr="true" name='p1'>
<a>[att=val]a>
div>
<div class="div" testattr="true" p2>
<a>[att]a>
div>
<div class="div" testattr="true" name="-">
<a>[att|=val]a>
div>
<div class="div" testattr="true" name="a b">
<a>[att~=val]a>
div>
div>
<script type="text/javascript">
$('div[name=p1]').css("border", "3px groove red");
script>
<script type="text/javascript">
$('div[p2]').css("border", "3px groove blue");
script>
<script type="text/javascript">
$('div[name|="-"]').css("border", "3px groove #00FF00");
script>
<script type="text/javascript">
$('div[name~="a"]').css("border", "3px groove #668B8B");
script>
<h3>[att^=val]、[att*=val]、[att$=val]、[att!=val]h3>
<div class="left" testattr="true" >
<div class="div" testattr="true" name='xiao-shitou'>
<a>[att^=val]a>
div>
<div class="div" testattr="true" name='shitou-xiao'>
<a>[att$=val]a>
div>
<div class="div" testattr="true" name="attr-test-selector">
<a>[att*=val]a>
div>
<div class="div" name="a b">
<a>[att!=val]a>
div>
div>
<script type="text/javascript">
$('div[name^="xiao"]').css("border", "3px groove red");
script>
<script type="text/javascript">
$('div[name$="xiao"]').css("border", "3px groove blue");
script>
<script type="text/javascript">
$('div[name*="test"]').css("border", "3px groove #00FF00");
script>
<script type="text/javascript">
$('div[testattr!="true"]').css("border", "3px groove #668B8B");
script>
body>
html>
子元素筛选选择器
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>title>
<link rel="stylesheet" href="choose.css" type="text/css">
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js">script>
head>
<body>
<h2>子元素筛选选择器h2>
<h3>:first-child、:last-child、:only-childh3>
<div class="left first-div">
<div class="div">
<a>:first-childa>
<a>第二个元素a>
<a>:last-childa>
div>
<div class="div">
<a>:first-childa>
div>
<div class="div">
<a>:first-childa>
<a>第二个元素a>
<a>:last-childa>
div>
div>
<script type="text/javascript">
$('.first-div a:first-child').css("color", "#CD00CD");
script>
<script type="text/javascript">
$('.first-div a:last-child').css("color", "red");
script>
<script type="text/javascript">
$('.first-div a:only-child').css("color", "blue");
script>
<h3>:nth-child、:nth-last-childh3>
<div class="left last-div">
<div class="div">
<a>:first-childa>
<a>第二个元素a>
<a>第三个元素a>
<a>:last-childa>
div>
<div class="div">
<a>:first-childa>
<a>第二个元素a>
div>
<div class="div">
<a>:first-childa>
<a>第二个元素a>
<a>第三个元素a>
<a>:last-childa>
div>
div>
<script type="text/javascript">
$('.last-div a:nth-child(2)').css("color", "#CD00CD");
script>
<script type="text/javascript">
$('.last-div a:nth-last-child(2)').css("color", "red");
script>
body>
html>
表单元素选择器
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>jQuery表单元素选择器title>
<link rel="stylesheet" href="choose.css" type="text/css">
<style>
input{
display: block;
margin: 10px;
padding:10px;
}
style>
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js">script>
head>
<body>
<h2>子元素筛选选择器h2>
<h3>input、text、password、radio、checkboxh3>
<h3>submit、image、reset、button、fileh3>
<div class="left first-div">
<form>
<input type="text" value="text类型"/>
<input type="password" value="password"/>
<input type="radio"/>
<input type="checkbox"/>
<input type="submit" />
<input type="image" />
<input type="reset" />
<input type="button" value="Button" />
<input type="file" />
form>
div>
<script type="text/javascript">
$(':input').css("border", "1px groove red");
script>
<script type="text/javascript">
$('input:text').css("background", "#A2CD5A");
script>
<script type="text/javascript">
$('input:password').css("background", "yellow");
script>
<script type="text/javascript">
$('input:radio').attr('checked','true');
script>
<script type="text/javascript">
$('input:checkbox').attr('checked','true');
script>
<script type="text/javascript">
$('input:submit').css("background", "#C6E2FF");
script>
<script type="text/javascript">
$('input:image').css("background", "#F4A460");
script>
<script type="text/javascript">
$('input:button').css("background", "red");
script>
<script type="text/javascript">
$('input:file').css("background", "#CD1076");
script>
body>
html>
表单对象选择器
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>title>
<link rel="stylesheet" href="choose.css" type="text/css">
<style>
input {
display: block;
margin: 10px;
padding: 10px;
}
style>
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js">script>
head>
<body>
<h2>子元素筛选选择器h2>
<h3>enabled、disabledh3>
<form>
<input type="text" value="未设置disabled" />
<input type="text" value="设置disabled" disabled="disabled" />
<input type="text" value="未设置disabled" />
form>
<script type="text/javascript">
$('input:enabled').css("border", "2px groove red");
script>
<script type="text/javascript">
$('input:disabled').css("border", "2px groove blue");
script>
<h3>checked、selectedh3>
<form>
<input type="checkbox" checked="checked">
<input type="checkbox">
<input type="radio" checked>
<input type="radio">
<select name="garden" multiple="multiple">
<option>码农option>
<option selected="selected">前端之乐option>
<option>活生生的生活option>
<option selected="selected">博客园option>
select>
form>
<script type="text/javascript">
$('input:checked').removeAttr('checked')
script>
<script type="text/javascript">
$('option:selected').removeAttr('selected')
script>
body>
html>
特殊选择器
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>title>
<link rel="stylesheet" href="imooc.css" type="text/css">
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js">script>
head>
<body>
<h2>特殊选择器thish2>
<p id="test1">click test:通过原生DOM处理p>
<p id="test2">click test:通过原生jQuery处理p>
<script type="text/javascript">
var p1 = document.getElementById('test1')
p1.addEventListener('click',function(){
this.style.color = "red";
},false);
script>
<script type="text/javascript">
$('#test2').click(function(){
$(this).css('color','blue');
})
script>
body>
html>
选择器综合练习
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery标签切换效果title>
<link rel="stylesheet" href="2-17.css" type="text/css">
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js">script>
head>
<body>
<div id="menu">
<div id="menu_female">
<h3>女装h3>
<div class="tag" style="display: block;">
<dl>
<dd>
<p>第一类p>
<a>1.衬衫a>
<a>2.T恤a>
<a>3.雪纺衫a>
<a>4.针织衫a>
<a>5.短外套a>
<a>6.卫衣a>
<a>7.小西裤a>
<a>8.风衣a>
<a>9.吊带背心a>
<a>10.连衣裙a>
<a name="setColor">11.蕾丝连衣裙a>
<a>12.复古连衣裙a>
<a>13.印花连衣裙a>
<a>14.真丝连衣裙a>
<a>更多a>
dd>
dl>
div>
<div class="tag_More" style="display:block">
<dl>
<dd>
<p>第二类p>
<a>1.背带裤a>
<a>2.哈伦裤a>
<a>3.牛仔裤a>
<a>4.休闲裤a>
<a>5.小脚裤a>
<a>6.西装裤a>
<a>7.打底裤a>
<a>8.阔脚裤a>
<a>9.短裤a>
<a>10.马甲/背心a>
<a>11.羽绒服a>
<a>12.棉服a>
<a>13.夹克a>
<a>14.POLO衫a>
<a>更多a>
dd>
dl>
div>>
div>
<div id="menu_con">
<h3>男装h3>
<div class="tag" style="display:block">
<dl>
<dd>
<p>第一类p>
<a>1.衬衫a>
<a>2.T恤a>
<a>3.牛仔裤a>
<a>4.休闲裤a>
<a>5.短裤a>
<a>6.针织衫a>
<a>7.西服a>
<a>8.西裤a>
<a>9.嘻哈裤a>
<a>10.西服套装a>
<a>11.马甲/背心a>
<a name="setColor">12.羽绒服a>
<a>13.棉服a>
<a>14.夹克a>
<p>更多p>
dd>
dl>
div>
<div class="tag_More" style="display:block">
<dl>
<dd>
<p>第二类p>
<a>1.衬衫a>
<a>2.T恤a>
<a>3.牛仔裤a>
<a name='setColor'>4.休闲裤a>
<a>5.短裤a>
<a>6.针织衫a>
<a>7.西服a>
<a>8.西裤a>
<a>9.嘻哈裤a>
<a>10.西服套装a>
<a>11.马甲/背心a>
<a>12.羽绒服a>
<a>13.棉服a>
<a>14.夹克a>
<p>更多p>
dd>
dl>
div>>
div>
div>
<script type="text/javascript">
$('#menu_con div.tag dd>p:first-child').css('color','red');
script>
<script type="text/javascript">
$('#menu_con>div:first dd:eq(0)>a:lt(4)').css('color','blue');
script>
<script type="text/javascript">
#('menu_con a[name="setColor"]').css('color','#66CD00');
script>
<script type="text/javascript">
$('#menu div.tag dd a:nth-child(10)').css('color','#C71585');
script>
<script type="text/javascript">
$('.tag:first a:contains("更多")')
script>
body>
html>