Blogger Information
Blog 16
fans 7
comment 1
visits 11479
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
12月17日- JS基础(二)
Eric
Original
500 people have browsed it

一、数组的定义和操作

  • 数组定义
  1. <script>
  2. //方法一
  3. var arr1 = new Array('java', 'php', 'python');
  4. //方法二
  5. var arr2 = ['java', 'php', 'python'];
  6. </script>
  • push(): 向数组末尾添加一个或多个元素,并返回新数组的长度
  1. <script>
  2. var arr = ['java', 'php', 'python'];
  3. var res = arr.push('html');
  4. console.log(res); // 输出:4
  5. </script>
  • pop(): 删除数组的最后一个元素并返回删除的元素
  1. <script>
  2. var arr = ['java', 'php', 'python'];
  3. var newArr = arr.push();
  4. console.log(newArr); // 输出:python
  5. </script>
  • shift(): 删除并返回数组的第一个元素
  1. <script>
  2. var arr = ['java', 'php', 'python'];
  3. var res = arr.shift();
  4. console.log(res); // 输出:java
  5. </script>
  • unshift(): 向数组的开头添加一个或更多元素,并返回新的长度
  1. <script>
  2. var arr = ['java', 'php', 'python'];
  3. var res = arr.unshift('css');
  4. console.log(res); // 输出:4
  5. </script>
  • slice(): 从已有的数组中返回选定的元素, 返回结果为数组
  1. <script>
  2. var arr = ['java', 'php', 'python'];
  3. var res = arr.slice(0, 2);
  4. console.log(res); // 输出:["java", "php"]
  5. </script>
  • splice(): 添加或删除数组中的元素, 返回结果为删除的元素数组
  1. <script>
  2. var arr = ['java', 'php', 'python'];
  3. var res = arr.splice(1, 0, 'html', 'css');
  4. console.log(res); // 输出:[]
  5. console.log(arr); // 输出:["java", "html", "css", "php", "python"]
  6. </script>
  • concat(): 连接两个或更多数组,并返回结果
  1. <script>
  2. var arr = ['java', 'php', 'python'];
  3. var arr2 = ['html', 'css'];
  4. var res = arr.concat(arr2);
  5. console.log(arr); // 输出:["java", "php", "python", "html", "css"]
  6. </script>
  • indexOf(): 返回一个元素在数组中下标
  1. <script>
  2. var arr = ['java', 'php', 'python'];
  3. var res = arr.indexOf('php');
  4. console.log(res); // 输出:1
  5. </script>
  • isArray(): 判断对象是否为数组
  1. <script>
  2. var arr = ['java', 'php', 'python'];
  3. var res = Array.isArray(arr);
  4. console.log(res); // 输出:true
  5. </script>
  • join(): 把数组转化为字符串,()内连接符可选
  1. <script>
  2. var arr = ['java', 'php', 'python'];
  3. var res = arr.join();
  4. console.log(res); // 输出:java,php,python
  5. </script>
  • includes(): 判断数组是否包含指定的值
  1. <script>
  2. var arr = ['java', 'php', 'python'];
  3. var res = arr.includes('php');
  4. console.log(res); // 输出:true
  5. </script>

二、DOM事件

  • onclick(): 当用户点击某个元素调用的事件
  1. <button onclick="show()">点击我</button>
  2. <script>
  3. function show() {
  4. alert('我被点击了');
  5. }
  6. </script>
  • ondbclick(): 当用户双击某个对象时调用的事件
  1. <button ondbclick="show()">点击我</button>
  2. <script>
  3. function show() {
  4. alert('我被双击了');
  5. }
  6. </script>
  • onmouseover(): 鼠标移到某元素之上
  1. <button onmousemove="show()">点击我</button>
  2. <script>
  3. function show() {
  4. alert('我移动到元素上了');
  5. }
  6. </script>
  • onmouseout(): 鼠标从某元素移开
  1. <button onmouseout="show()">点击我</button>
  2. <script>
  3. function show() {
  4. alert('我从元素上移开了');
  5. }
  6. </script>
  • onblur(): 失去焦点时触发
  1. <input type="text" onblur="show()">
  2. <script>
  3. function show() {
  4. alert('失去焦点时触发');
  5. }
  6. </script>
  • onchange(): 内容改变时触发
  1. <select name="city" id="city" onchange="show()">
  2. <option value="1">北京</option>
  3. <option value="2">上海</option>
  4. <option value="3">广州</option>
  5. <option value="4">深圳</option>
  6. </select>
  7. <script>
  8. function show() {
  9. var text = document.getElementById('city').value;
  10. alert(text);
  11. }
  12. </script>

THE END !

Correction status:Uncorrected

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!