Blogger Information
Blog 5
fans 0
comment 0
visits 3202
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JavaScript数组函数slice()与splice()方法的使用及引入方式2019-01-17 17:20
王jc的博客
Original
552 people have browsed it

一、slice()和splice()的使用

①、slice()

实例

<script>
var arr = [1,2,3,4,5,6]; // 返回从索引1到4之间元素(不含索引4)

alert( arr.slice(1,4) );  // [2,3,4]

alert( arr.slice(1,-2) );  // [2,3,4],尾部从-1开始
</script>

运行实例 »

点击 "运行实例" 按钮查看在线实例

②、splice()

实例

<script>
// 增加和删除元素,返回删除的元素数组

// splice(开始索引, 要删除的数量(0表示不删除), 要添加的元素(多个逗号分开))

var arr = [1,2,3,4,5,6,7,8];

arr.splice(2,0,'a'); // 从第三个位置添加一个元素,非删除操作,返回空数组

alert(arr);  // [1, 2, "a", 3, 4, 5, 6, 7, 8]

arr.splice(4,2);  // 从第5个位置删除2个,返回 [4, 5]

alert(arr); // [1, 2, "a", 3, 6, 7, 8]

// 更新,实际上分二步完成:1.删除:是先删除指定元素, 2.添加: 再添加新元素到这个位置上

arr.splice(2,1,'php');  // ['a']
alert(arr); // [1, 2, "php", 3, 6, 7, 8]
</script>

运行实例 »

点击 "运行实例" 按钮查看在线实例

 

二、js引入方式

行内引入:直接在元素的事件方法属性上写js代码

<body>
<input type="button" onclick="alert('行内引入')" value="button" name="button">
<button onclick="alert(123)">点击我</button>
</body>

内部引入:将js脚本写script标签中,仅限当前页面使用

<script>
alert("这是js的内部引入");
</script>

外部引入:将js脚本写到外部的js文件中

定义外部js文件(.js结尾的文件)

<scripttype="text/javascript" src="demo.js"></script>

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!