34前端基础 -JQuery之遍历

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_20042935/article/details/88690744

JQuery遍历

两种遍历方式:

jquery对象.each(function(index,ele){
		//this	  遍历后的结果   js对象
		//ele     遍历后的结果   js对象
		//index   索引
})

$.each(jquery对象,function(index,ele){
				//this	  遍历后的结果   js对象
				//ele     遍历后的结果   js对象
				//index   索引
	})

案例:遍历隐藏域的值

直接上代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>05-可见性过滤选择器.html</title>

		<script src="../js/jquery-1.11.0.min.js"></script>
		<script type="text/javascript">
			$(function() {

				$("#b1").click(function() {

					/*
					 * 方式一:
					 * 
					 * $("[type=hidden]").each(function() {
						alert(this.value);
					});
					
					* 
					* */

					//方式二:
					$("[type=hidden]").each(function(index, val) {
						alert(index + " : " + val.value);
					});

				})

				$("#b2").click(function() {
					$.each($("[type=hidden]"), function(index, val) {
						alert(index + " : " + val.value);
					});
				})

			});
		</script>
	</head>

	<body>

		<input type="button" value=" 选取所有的文本隐藏域, 并打印它们的值" id="b1" />
		<input type="button" value=" 选取所有的文本隐藏域, 并打印它们的值" id="b2" />
		<br /><br />

		<!--文本隐藏域-->
		<input type="hidden" value="hidden_1">
		<input type="hidden" value="hidden_2">
		<input type="hidden" value="hidden_3">
		<input type="hidden" value="hidden_4">

	</body>

</html>

猜你喜欢

转载自blog.csdn.net/qq_20042935/article/details/88690744