Blogger Information
Blog 32
fans 1
comment 0
visits 23129
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1218日js对象js选择器-PHP培训第九期线上班
淡月
Original
958 people have browsed it

一.js对象

定义对象方法属性

  1. <script>
  2. // 创建对象1
  3. var obj1 = new Object();
  4. obj1.name = 'danyue';
  5. obj1.age = 22;
  6. obj.show = function(){
  7. console.log('我是obj1 的方法');
  8. };
  9. console.log(obj.name); // 输出:danyue
  10. console.log(obj.show()); //输出:我是obj1 的方法
  11. // 创建对象2
  12. var obj2 = {name: "danyue", age: 22};
  13. obj2.sex = '男';
  14. console.log = obj2.name; //输出:{name: "danyue", age: 22, sex: "男"}
  15. // 创建对象3
  16. var obj3 = {
  17. name: 'danyue',
  18. age: 22,
  19. show: function (val) {
  20. console.log(val + '我是show 方法');
  21. }
  22. };
  23. console.log(obj3.show('hello, ')); //输出:hello, 我是show 的方法
  24. //对象内部方法调用内部方法
  25. var obj4 = {
  26. name: danyue,
  27. age: 22,
  28. getName = function(){
  29. console.log(obj4.name);
  30. },
  31. getInfo = function(){
  32. obj4.getName();
  33. }
  34. }
  35. console.log(obj4.getInfo()); //输出:danyue
  36. </script>

二.js定时器

延长器:setTimeout(), setInterval(), clearInterval()

setTimeout:只执行一次,setInterval:可以执行多次,clearInterval清除定时器

  1. // setTimeout
  2. setTimeout(function () {
  3. console.log('我是定时器');
  4. },3000)
  5. function show(){
  6. console.log('3秒后再执行我');
  7. }
  8. setTimeOut(show,3000);
  9. // setinterval
  10. setInterval(function(){
  11. console.log(new Date());
  12. },1000);
  13. // 设定程序4秒后停止执行
  14. var time = setInterval(function () {
  15. console.log(new Date());
  16. }, 1000);
  17. setTimeout(function () {
  18. clearInterval(time);
  19. },4000);

发送验证码小案例

  1. <button id="text">发送验证码</button>
  2. <script>
  3. var button = document.getElementById('text');
  4. button.addEventListener('click',function () {
  5. var text = document.getElementById('text').textContent;
  6. var time = 6;
  7. var val = setInterval(function () {
  8. document.getElementById('text').textContent = time+'秒后重新发送';
  9. time --;
  10. if (time == 0){
  11. clearInterval(val);
  12. document.getElementById('text').textContent = text;
  13. }
  14. },1000);
  15. });
  16. </script>
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