JQuery学习17篇(jQuery对象的遍历)

本文介绍了一种使用JQuery进行对象遍历的方法,通过具体的示例代码展示了如何实现复选框的反选功能,包括面向过程和函数式的实现思路。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JQuery学习17篇(jQuery对象的遍历)</title>
<link rel="stylesheet" type="text/css" href="inputAndDiv.css">
<style type="text/css">
input[type=button] {
	width: 100px;
}

input[type=checkbox] {
	width: 20px;
	height: 20px;
}
</style>
</head>
<body style="background-color: #CCE8CF;">
	<h3 style="color: #cd1636;">JQuery学习17篇(jQuery对象的遍历)</h3>
	<input type ="button"value="反选" onclick="opposite()"/><br/>
		<p>令狐冲:
		<input type ="checkbox" value="linghuchong"/></p>
		<p>韦小宝:
		<input type ="checkbox" value="weixiaobao"/></p>
		<p>张无忌:
		<input type ="checkbox" value="zhangwuji"/></p>
		<p>杨过:
		<input type ="checkbox" value="yangguo"/></p>
		<p>郭靖:
		<input type ="checkbox" value="guojing"/></p>
</body>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
//面向过程的思路
/*
function opposite() {
	var checkboxs = $('input:checkbox');
	for (var index = 0; index < checkboxs.length; index++) {
		if ($(checkboxs[index]).prop('checked')) {
			$(checkboxs[index]).prop('checked', false);
		}else{
			$(checkboxs[index]).prop('checked', true);
		}
	}
}
*/
/*
function opposite() {
	var checkboxs = $('input:checkbox');
	for (var index = 0; index < checkboxs.length; index++) {
		if (checkboxs[index].checked) {
			checkboxs[index].checked = false;
		}else{
			checkboxs[index].checked = true;
		}
	}
}
*/

//面向函数式思路,回调函数
//在回调函数中, this是谁? 
//在循环过程中, 循环到哪个DOM对象, 回调函数中的this就指向哪个DOM对象
/*
function opposite() {
	$('input:checkbox').each(function(){
		console.log(this);
		if ($(this).prop('checked')) {
			$(this).prop('checked', false);
		}else{
			$(this).prop('checked', true);
		}
	});
}
*/

/*
function opposite() {
	$('input:checkbox').each(function() {
		console.log(this);
		if (this.checked) {
			this.checked = false;
		} else {
			this.checked = true;
		}
	});
}
*/


function opposite() {
	$('input:checkbox').each(function() {
		console.log(this);
		//简化写法
		this.checked = !this.checked;
	});
}
</script>
</html>

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值