一.js數(shù)組的常用函數(shù)(slice()和splice())
var arr = [1,2,3,4,5,6]; // slice查詢 slice(起點,終點) arr.slice(1,4) ;//取第2到第5 之間的值 包括起點 不包括終點 console.log(arr.slice(1,4)); arr.slice(1,-2); // 可倒數(shù)取值 console.log(arr.slice(1,-2)); //splice 可增刪查改 // splice(起點,刪除的數(shù)量,需要添加刪除的數(shù)據(jù)(可選)) // 添加 arr.splice(2,0,'QQ'); //從第3個 不刪除數(shù)據(jù) 添加 QQ console.log(arr) //刪除 返回被刪除的值 arr.splice(4,2); //從第5個值 開始刪2個 console.log(arr); //更新 刪除當(dāng)前 添加新數(shù)據(jù) arr.splice(2,1,'WW'); console.log(arr); //分割 字符串 成數(shù)組 var str ='html,css,javascript' var strArr= str.split(','); console.log(strArr);
二. js引入到html文檔中的三種方式
<!-- 1.直接左元素的事件方法屬性上寫JS代碼 -->
<h2 onclick="alert('我喜歡PHP中文網(wǎng)')">PHP中文網(wǎng)</h2>
<!-- 2.將JS寫到script標(biāo)簽里面 -->
<form action="">
<input type="text" name="username" placeholder="PHP中文網(wǎng)">
<button type="button" onclick= "check(username)">驗證</button>
</form>
<script>
function check(username){
//驗證非空(判斷長度 是否等于0)
if(username.value.length === 0){
alert('用戶名為空-請輸入');
} else {
alert('驗證成功');
}
}
</script>
<!-- 3.將JS腳本寫到外部JS文件中 -->
<script src="js.js"></script>
總結(jié):
數(shù)組操作函數(shù) slice()splice())
slice()有兩個參數(shù) 查詢的 (起點, 終點) (但結(jié)果不包括終點)
splice()有三個參數(shù) (起點,刪除的數(shù)量,添加的數(shù)據(jù))
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號