Blogger Information
Blog 8
fans 0
comment 0
visits 6312
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS引入及组的常用函数(slice()和splice())(实例) 2019年1月16日 20:00
默默然O_o的博客
Original
696 people have browsed it

一.js数组的常用函数(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); // 可倒数取值
        console.log(arr.slice(1,-2));

        //splice 可增删查改
        // splice(起点,删除的数量,需要添加删除的数据(可选))

        // 添加
        arr.splice(2,0,'QQ'); //从第3个 不删除数据 添加 QQ
        console.log(arr)

        //删除    返回被删除的值
        arr.splice(4,2); //从第5个值 开始删2个
        console.log(arr);

        //更新   删除当前 添加新数据
        arr.splice(2,1,'WW');
        console.log(arr);

        


        //分割 字符串 成数组

        var str ='html,css,javascript'
        var strArr= str.split(',');
        console.log(strArr);

 

二. js引入到html文档中的三种方式


 

     <!-- 1.直接左元素的事件方法属性上写JS代码 -->
    <h2 onclick="alert('我喜欢PHP中文网')">PHP中文网</h2>

    <!-- 2.将JS写到script标签里面 -->
    <form action="">
        <input type="text" name="username" placeholder="PHP中文网">
        <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>

 

总结:

数组操作函数 slice()splice())

  1.  slice()有两个参数 查询的 (起点, 终点)     (但结果不包括终点)

  2. splice()有三个参数  (起点,删除的数量,添加的数据)

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