Jquery选择器练习(二)

Jquery的选择器非常强大,只要运用熟悉,方可对Dom操作得心应手。一些选择器和CSS伪类很相似,很容易引起混淆,需要多加练习和注意。

<!DOCTYPE html>
<html lang="zh-cn">
	<head>
		<title>Test</title>
		<meta charset="UTF-8" />
		<script src="js/jquery-1.11.1.min.js"></script>
		<script src="js/index.js"></script>
		<script type="text/javascript">
			$(document).ready(function() {
				initElementBgColor();
			});
			var initElementBgColor = function() {
				$("div.smallBox:odd").css("background", "#ccc");
				$("div.smallBox:even").css("background", "#ccc");
				$(".smallBox:first").css("background", "#ccc");
				$(".smallBox:last").css("background", "#ccc");
				$(".smallBox:nth-child(2)").css("background", "#ccc");
				$(".smallBox:nth-child(n+1)").css("background", "#ccc");
				$(".smallBox:nth-child(2n)").css("background", "#ccc");
				$(".smallBox:nth-child(2n+1)").css("background", "#ccc");
				$(".smallBox:nth-child(-3n+100)").css("background", "#ccc");
				$(".smallBox:gt(3)").css("background", "#ccc");
				$(".smallBox:lt(3)").css("background", "#ccc");
				$(".smallBox:eq(5)").css("background", "#ccc");
				$(".smallBox:not(:empty)").css("background", "#ccc");
				$(".smallBox:hidden").css("display", "block");
				$(".smallBox:not(:visible)").css("display", "block");
				$(".box .smallBox,:visible").css("display", "block");
				$(".box .smallBox p:contains(4)").css("background", "#ccc");
				$(".box :header").css("background", "#ccc");
			}
		</script>

		<style type="text/css" >
			* {margin: 0;padding: 0;}
			.box {border: 1px solid;}
			.smallBox {
				width: 100px;
				height: 100px;
				border: 1px solid red;
				margin-left: 91px;
				float: left;
				font-size: 80px;
				line-height: 100px;
				text-align: center;
			}
			.clear {
				clear: both;
				height: 0px;
				overflow: hidden;
			}
			.show {display: none;}
			.able {visibility: hidden;}
		</style>
	</head>
	<body>
		<div class="box">
			<div class="smallBox">1</div>
			<div class="smallBox">2</div>
			<div class="smallBox">3</div>
			<div class="smallBox">
				<p>4</p>
			</div>
			<div class="smallBox">5</div>
			<div class="smallBox">6</div>
			<div class="smallBox">7</div>
			<div class="smallBox show">8</div>
			<div class="smallBox able">9</div>
			<div class="clear"></div>
			<div>
				<h1>H1H1H1H1H1H1H1H1H1H1H1H1H1H1H1</h1>
				<h2>H2H2H2H2H2H2H2H2H2H2H2H2H2H2H2</h2>
			</div>
		</div>
	</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值