1. 程式人生 > >JQuery查詢子元素find()和遍歷集合each的方法積累

JQuery查詢子元素find()和遍歷集合each的方法積累

1.HTML程式碼

<div name="students" school="HK">
		<input type="boy" name="ZhangSan" value="206">
		<input type="girl" name="Lisi" value="108">
	</div>

2.jquery
<script type="text/javascript">
	/* find() 查詢子元素方法 */
		var aaa = $("div[name='students'][school='HK']").find("input[type='boy'][name='ZhangSan']");
		console.log(aaa.val());
		
	/* $(".child",parent); 方法查詢子元素*/
		var bbb = $($("input[type='boy'][name='ZhangSan']"),$("div[name='students'][school='HK']"));
		console.log(aaa.val());

		/*  each()方法遍歷陣列 */
		var arr = [ "one", "two", "three", "four" ];
		$.each(arr, function() {
			console.log(this);
		});
		
		/* each()方法處理json */
		var obj = {
			one : 1,
			two : 2,
			three : 3,
			four : 4
		};
		$.each(obj, function(key, val) {
			console.log(obj[key]);
		});
		
		/* each()方法處理二維陣列 */
		var arr1 = [ [ 11, 44, 33 ], [ 4, 6, 6 ], [ 7, 20, 9 ] ]
		$.each(arr1, function(i, item) {
			console.log(item[0]);
		});

		/* each()方法處理HTML元素 */
		$("div[name='students'][school='HK'] > input").each(function() {
			console.log($(this).val());
		});
	</script>