1.1 全局对象

  1. <!-- 全局变量 -->
  2. <script>
  3. /* 中javascript中声明的全局变量是window的属性,方法是window的方法 */
  4. var a = 10;
  5. function go(){
  6. console.log("hello world")
  7. }
  8. // const window = {
  9. // a:10,
  10. // go:function(){
  11. // console.log("hello world")
  12. // }
  13. // }
  14. // console.log(window.a);
  15. // window.go();
  16. console.log(a);
  17. go();
  18. </script>

1.2 弹窗

1.2.1 有确定取消类

  1. <!-- confirm弹窗 -->
  2. <ul>
  3. <li>html <button>删除</button></li>
  4. <li>css <button>删除</button></li>
  5. <li>javascript <button>删除</button></li>
  6. </ul>
  7. <script>
  8. // parentNode --元素的父节点
  9. var btns = document.getElementsByTagName("button");
  10. for(var i=0;i<btns.length;i++){
  11. btns[i].onclick = function(){
  12. var temp = window.confirm("确定吗");
  13. if(temp){
  14. this.parentNode.style.display = "none"
  15. }
  16. }
  17. }
  18. </script>

1.2.2 输入型弹窗

  1. <!-- 输入型弹窗 -->
  2. <script>
  3. /* 输入型的弹窗 */
  4. var test = window.prompt("请输入","hello world");
  5. console.log(test)
  6. </script>

1.3 定时器

  1. <!-- 定时器 -->
  2. <script>
  3. // 超时调用,只会触发一次
  4. setTimeout(function(){
  5. console.log("大帅b");
  6. },5000)
  7. // 间歇调用
  8. setInterval(() => {
  9. console.log("我tm怎么那么帅!");
  10. }, 1000);
  11. // 用递归方法,让超时调用实现间歇调用
  12. function show(){
  13. setTimeout(function(){
  14. console.log("诶!我又跳出来了。");
  15. show()
  16. },1000)
  17. };
  18. show();
  19. // 清除定时器
  20. var time = setInterval(() => {
  21. console.log("我tm怎么那么帅!");
  22. }, 1000);
  23. clearInterval(time);
  24. </script>

1.4 添加window监听事件

  1. window.addEventListener("scroll",函数)

使用了记得一定一定要移除,不然事件会一直存在

  1. window.removeEventListener("scroll",函数)