Blogger Information
Blog 34
fans 2
comment 0
visits 23084
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
12月17号作业 JS数组及JS事件
遗忘了寂寞
Original
729 people have browsed it

定义数组

  1. <script type="text/javascript">
  2. //定义数组
  3. var arr=['a','b','d'];
  4. //控制台输出
  5. console.log(arr);
  6. </script>

数组尾部添加元素

  1. <script type="text/javascript">
  2. //定义数组
  3. var arr=['a','b','d'];
  4. //数组尾部添加元素
  5. //返回值是数组的长度
  6. arr.push('e');
  7. console.log(arr);
  8. </script>

从数组尾部弹出一个元素

  1. <script type="text/javascript">
  2. //定义数组
  3. var arr=['a','b','d'];
  4. //从数组尾部弹出一个元素
  5. //返回值是被删除的元素
  6. arr.pop();
  7. console.log(arr);
  8. </script>

数组头部添加元素

  1. <script type="text/javascript">
  2. //定义数组
  3. var arr=['a','b','d'];
  4. //数组头部添加元素
  5. //返回值是数组的长度
  6. arr.unshift('e');
  7. console.log(arr);
  8. </script>

数组头部弹出一个元素

  1. <script type="text/javascript">
  2. //定义数组
  3. var arr=['a','b','d'];
  4. //数组头部弹出一个元素
  5. //返回值是被删除的元素
  6. arr.shift();
  7. console.log(arr);
  8. </script>

从数组中删除元素

  1. <script type="text/javascript">
  2. //定义数组
  3. var arr=['a','b','c','d'];
  4. //从数组中删除元素
  5. // splice(从哪里开删除,删除的长度)
  6. arr.splice(1,2);
  7. console.log(arr);
  8. </script>

从数组中查找元素

  1. <script type="text/javascript">
  2. //定义数组
  3. var arr=['a','b','c','d'];
  4. //从数组中查找元素
  5. //indexOf('要查找的元素','从哪开始查找')
  6. //有的话返回下标,没有返回-1
  7. var res = arr.indexOf('c');
  8. console.log(res);
  9. </script>


onclick事件

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <div>
  9. <label>用户:</label><input type="text" name="" id="usename" value="" />
  10. </div>
  11. <div>
  12. <label>密码:</label><input type="password" name="" id="password" value="" />
  13. </div>
  14. <button onclick="submits()">提交</button>
  15. <script type="text/javascript">
  16. function submits(){
  17. var usename=document.getElementById('usename').value;
  18. var password=document.getElementById('password').value;
  19. if(usename==''){
  20. alert('请输入用户名');
  21. return;
  22. }
  23. if(password==''){
  24. alert('请输入密码');
  25. return;
  26. }
  27. alert('用户: '+usename+' 欢迎登录');
  28. }
  29. </script>
  30. </body>
  31. </html>




onmousemove,onmouseleave事件

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <div onmousemove="aa()" onmouseleave="bb()">鼠标移到这里看看</div>
  9. <script type="text/javascript">
  10. function aa(){
  11. console.log('鼠标移上来了');
  12. }
  13. function bb(){
  14. console.log('鼠标移走了');
  15. }
  16. </script>
  17. </body>
  18. </html>

onblur事件

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <div>
  9. <label>用户:</label><input type="text" name="" id="usename" value="" onblur="usename()"/>
  10. </div>
  11. <div>
  12. <label>密码:</label><input type="password" name="" id="password" />
  13. </div>
  14. <button>提交</button>
  15. <script type="text/javascript">
  16. function usename(){
  17. var usename=document.getElementById('usename').value;
  18. if(usename==''){
  19. alert('请输入用户名');
  20. return;
  21. }
  22. }
  23. </script>
  24. </body>
  25. </html>


onchange事件

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <select id="province" onchange="aa()">
  9. <option value ="1">北京</option>
  10. <option value ="2">上海</option>
  11. <option value ="3">广州</option>
  12. <option value ="4">深圳</option>
  13. </select>
  14. <script type="text/javascript">
  15. function aa(){
  16. var province=document.getElementById('province').value;
  17. alert('你当前选择的是: ' + province);
  18. }
  19. </script>
  20. </body>
  21. </html>

Correcting teacher:天蓬老师天蓬老师

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!