学如逆水行舟,不进则退
ID选择器、类选择器、标签选择器、通配符选择器、多项选择器
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="../../jquery-3.6.0.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="one"></div>
<div id="two"></div>
<div id="three"></div>
<div class="four"></div>
<div id="five"></div>
<div class="six"></div>
<script type="text/javascript">
$('div').text("标签选择器");
$('#one').text("one")
$('.four').text("four");
var all=$('*');
console.log(all);
$('#five,.six').text("多项选择器");
</script>
</body>
</html>
后代选择器
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="../../jquery-3.6.0.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
<p>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<nav>
<span></span>
<span></span>
</nav>
</p>
<script type="text/javascript">
$('div span').text("div下面的span");
$('p>span').text("P下面的子元素");
</script>
</body>
</html>
属性选择器
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="../../jquery-3.6.0.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div class="one">1
<span class="one_span">2</span>
</div>
<div class="two">3
<span class="two_span" id="two_span_id">4</span>
</div>
<div class="three">5
<span class="three_span">6</span>
</div>
<script type="text/javascript">
$('[class]').css('color','red');
$('[class=one]').css('color','blue');
$('[class!=two]').css('color','blue');
$('[class^=two]').css('color','yellow');
$('[class$=span]').css('color','green');
$('[class*=e]').css('color','pink');
$('[class][id]').css('color','gray');
</script>
</body>
</html>
child选择器
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="../../jquery-3.6.0.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div>1
<a href="">3</a>
<span>2</span>
<span>4</span>
<ul>
<li>5</li>
<li>6</li>
<li>7</li>
</ul>
</div>
<div>
<span>8</span>
<span>9</span>
<span>10</span>
</div>
<script type="text/javascript">
var first=$('div span:first-child');
console.log(first);
var last=$('div span:last-child');
console.log(last);
var nth=$('div span:nth-child(2)');
nth.css('color','red');
console.log(nth);
</script>
</body>
</html>
表单相关选择器
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="../../jquery-3.6.0.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<input type="text" name="" id="" value="" />
<input type="password" name="" />
<input type="button" name="" id="" value="" disabled=""/>
<button type="button"></button>
<select name="">
<option value=""></option>
</select>
<textarea rows="" cols=""></textarea>
<script type="text/javascript">
var input=$(':input');
console.log(input);
var text=$(':text');
console.log(text);
var disabled=$(':disabled');
console.log(disabled);
</script>
</body>
</html>
|