
批改狀態(tài):合格
老師批語(yǔ):數(shù)組遍歷, 或者類數(shù)據(jù)遍歷是編程中非常常用, 特別是jquery, 就是完全基于它實(shí)現(xiàn)的
1、代碼練習(xí)demo1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="../JQuery3.5.1.js"></script>
<title>Document</title>
</head>
<body>
<ol id="first">
<li>1item1</li>
<li>1item2</li>
<li>1item3</li>
<li>1item4</li>
<li>1item5</li>
</ol>
<ol id="second">
<li>2item1</li>
<li>2item2</li>
<li>2item3</li>
<li>2item4</li>
<li>2item5</li>
</ol>
</body>
<script defer>
// $()通過(guò)CSS來(lái)選擇DOM節(jié)點(diǎn)
var cl = console.log.bind(console);
$("li").last().css("color", "red");
cl($("li", "#second"));
//$()也可以操作原生JS對(duì)象
var ol = document.querySelectorAll("ol");
cl(ol);
$(ol).css("background", "lightgreen");
//$()操作html
$("<h3>JQuery<h3>").prependTo("body"); //頭部插入
$("<hr>").appendTo("body"); //尾部插入
// $(callback)當(dāng)HTML加載完畢后執(zhí)行回調(diào)函數(shù)
var content = "加載完畢";
// $(function () {
// alert(content);
// });
// $()屬性和方法的使用
cl("……………………………………");
cl($("li"));
cl($("li").toArray());
cl($("li").length);
cl($("li")[4]);
cl($("li").get(0));
// 靜態(tài)方法直接作用于$上的方法
$.each($("li"), function (index, value) {
cl(index, value);
});
$("li").each(function (index, value) {
if (index % 2) cl(value);
});
//toArray()轉(zhuǎn)換數(shù)組方法
var arr = $("#second li").toArray();
cl(arr);
$("#second li").each(function (index, value) {
cl("第" + (index + 1) + "元素:", value);
});
cl("^^^^^^^^^");
//數(shù)組遍歷forEach()與each()遍歷函數(shù)
arr.forEach(function (value, index) {
cl("第" + (index + 1) + "個(gè):", value);
});
cl($("li"));
cl($("li").length);
cl($("li").last().index());
//$.map()靜態(tài)方式(必須有返回值)
cl("^^^^^^^^^^^^^^^^^");
var jquerystr = $.map($("li"), function (value, index) {
return value;
});
cl(jquerystr);
</script>
</html>
演練結(jié)果:
2、demo2代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="../JQuery3.5.1.js"></script>
</head>
<body>
<form action="" method="POST">
<label for="">處理方式:</label><br />
<label for="edit">編輯</label>
<input type="radio" name="type" id="edit" value="1" checked /><br />
<label for="amend">修改</label>
<input type="radio" name="type" id="amend" value="0" /><br />
<button onclick="return false">提交</button>
</form>
</body>
<script>
var cl = console.log.bind(console);
$("button").click(function () {
// $("input[id='edit']").prop("checked");//動(dòng)態(tài)判斷radio是否認(rèn)為選中,選中為true,沒(méi)有選中為false;
if ($("input[id='edit']").prop("checked")) {
$("form").attr("action", "handle.php?type=edit");
} else {
$("form").attr("action", "handle.php?type=amend");
}
});
</script>
</html>
運(yùn)行效果圖
1、jQuery 是一個(gè)非常流行的 JavaScript 函數(shù)庫(kù),主要用于DOM查詢操作和Ajax以及動(dòng)畫常用操作;
2、JQuery所有操作主要依賴于$()
工廠函數(shù)的屬性和方法;
3、靜態(tài)方法是指可以直接作用于$
上的方法
4、$(CSS選擇器)
:用來(lái)選擇DOM節(jié)點(diǎn)
5、$()
也可把html和js對(duì)象轉(zhuǎn)換成jquery對(duì)象使用$
中的方法和屬性;
6、$().appendTo():把某添加到某中:尾部添加;$().prependTo():把某添加到某中:頭部添加;
7、$()的屬性:lenght 內(nèi)容數(shù)量(長(zhǎng)度)
8、$()上的方法:get()獲取子元素;toArray()轉(zhuǎn)換成數(shù)組;prop("checked")
動(dòng)態(tài)判斷radio是否認(rèn)為選中,選中為true,沒(méi)有選中為false;css()
來(lái)選擇或者設(shè)置CSS樣式,可以通過(guò)子JS對(duì)象的方式設(shè)置多個(gè)樣式;attr()
:來(lái)選擇或設(shè)置元素的相關(guān)屬性;
9、靜態(tài)方法:$.each(JQuery,callback)
遍歷Jquery對(duì)象(參數(shù):index,value);$.map(jquery,callback)
遍歷Jquery對(duì)象必須有返回值(參數(shù):value,index);
10、數(shù)組遍歷:forEach(function(value,index){……})
數(shù)組遍歷
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)