Blogger Information
Blog 5
fans 0
comment 0
visits 4992
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js数组的常用函数(slice()和splice())和js引用的三种方法总结—2019年1月16日
阿芃达个人博客
Original
1230 people have browsed it

总结:

slice()和splice()

slice(参数1,参数2)可以查找数组下对应的数据,参数1为起始位置,参数2为结束位置,参数2可以为负数,-1对应的是从后向前数的第一个数值。splice()可以进行增删改查数据操作,splice(参数1,参数2,参数3),其中参数1是数值的起始位置,参数2为0是在起始位置之后增添新的数值,为正数(1即是添加一个数值,以此类推)是在起始位置之后添加新的数值,为负数(-1即是添加一个数值,以此类推)是在起始位置之前添加新的数值,如果数值超出当前数组的数值时,也会添加,直到添加完所有数值位置,参数3是数值的具体内容,可以为任何数据类型(字符串string,数值number,布尔值boolen,对象object,数组array,函数function,空值null,未定义值undefined)



实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        var a = [1,2,3,4,5,6,7]
        console.log(a.slice(1,4))
        console.log(a.slice(0,-3))
        console.log(a.splice(3,0,'javascript'))
        console.log(a)
        console.log(a.splice(4,2))
        console.log(a)
        console.log(a.splice(3,2,'php','css'))
        console.log(a)
    </script>
</body>
</html>

运行实例 »

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




实例

<!DOCTYPE html>
<html lang="en">
<head><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>外部通过引用jq库练习,可以多个文件调用</title>
</head>
<body>
    <div>直接在元素属性上写js代码</div>
    <form action="">
        <input type="text" name="site" value="php中文网">
        <button onclick="console.log(site.value)">点击</button>
    </form>
    <script>
        function a() {
            alert('我是通过当前页面script的代码实现的')
        }
        a()
    </script>
</body>
</html>

运行实例 »

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



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!