首页 > 其他分享 >2022-08-25 第二组刘禹彤 学习笔记

2022-08-25 第二组刘禹彤 学习笔记

时间:2022-08-25 18:44:54浏览次数:77  
标签:function 25 console log 08 刘禹彤 let div 函数

打卡40天

###学习内容

Javascript

自定义属性

获取属性

  • 所有的html元素,我们可以根据具体需求,自定义添加属性
  • 元素.属性名的方式只适用于元素原生的属性

自定义属性

div getAttribute("属性")
  • 设置属性(键值对)
div.setAttribute("属性",“属性名‘)
  • 删除属性
div.removeAttribute("属性")
  • 拿到元素所有属性

返回的结果是一个属性节点的映射的集合

console.log(div.Attributes)
  • 拿到其中某一个节点的子节点
console.log(div.Attributes[index].xxx)

BOM(Browser Object Model)

BoM核心对象-window

  • navigator
  • history
  • screen
  • location

回调函数

回调函数:一个函数的参数是另一个函数

let calllback = function(a{
console.log(a)
}
//传数字
console.log(1)
//传字符串
console.log("hello")
//传对象
console.log({name:"zhangsan"})
//传数组
console.log([1,2,3])

test函数中,入参可以是一个函数

  let test = function(fun){
                console.log("before");
                // 调用了传进来的函数
                fun(1000);
                console.log("after");
            }
            let callback = function(data) {
                console.log("I am callback," + data);
            }
            // test(1)
            test(callback);

传入的参数是一个函数类型时,只需要写函数名,不需要写()

定时函数

  • 定义计时函数

参数1:函数

参数2:延迟时间

  let timer = setTimeout(function(){
                document.write("<h1>5秒钟后可以见到我</h1>")
            },5000);
  • 清除计时函数
 clearTimeout(timer);
  • 周期性定时器
 let timer = setInterval(function() {
                console.log("123");
            },2000);
            clearInterval(timer);

浏览器

  • 浏览器自带了一个小型的数据库

浏览器自带的一个map集合,永久保存

  • 向Storage中设置键值对
  window.localStorage.setItem("name","lucy");
            window.localStorage.setItem("age",25);
  • 从Storage中根据键获取值
 console.log(localStorage.getItem("name"));
  • 从Storage中删除对应的键值对
localStorage.removeItem("name");

从控制台删除此键值对

  • 清空Storage中所有的键值对
 localStorage.clear();

弹窗

  • 警告弹窗
alert(1);
  • 带有确认和取消的弹窗

点击确定,返回true,点击取消,返回false

let flag = confirm("你是好人吗?"); 
 alert(flag);
  • 带有文本框的弹窗

文本框输入的内容就是返回值

 let name = prompt("请输入你的名字:","例如:张三");
   alert(name);

这三个弹窗开发中基本不用!!!

  1. 不好看
  2. 不同浏览器长得不一样
  3. 弹窗位置无法修改

history

历史记录

  function fun() {
                // history.forward();
                // 退 退 退
                 history.back();
                // 既可以前进,又可以后退
                // 传入的参数为正,则前进
                // 传入的参数为负,则后退
                // 参数就是步数,走几步
                history.go(2);
            }

location

获取页面路径

  function fun() {
                // 值是要跳转页面的路径
                // 可以是相对路径,也可以是绝对路径,还可以是http地址
            location.href = "demo02.html";
            location.href = "http://www.baidu.com";
                // 取值
                 alert(location.href);
                // 刷新页面
                location.reload();
           }

screen

           console.log(screen.width);
           console.log(screen.height);
           console.log(screen.availHeight);
           console.log(screen.availWidth);
           

navigater

获取一些浏览器的参数

            console.log(navigator.platform);
            console.log(navigator.appVersion);
            console.log(navigator.appName);

window

    <body>
        <button onclick="fun()">关闭</button>
        <script>
            function fun() {
                // window.close();
                // window.open("demo01.html");
                window.print();
            }
        </script>
    </body>

事件

添加事件

方式一

/ 获取指定的元素
           let div = document.querySelector("div");
           /*
                参数1:要添加的事件类型
                参数2:添加的事件要触发的函数


                传说中,这种添加事件的方式有兼容性问题。
           */
           div.addEventListener("click",function(){
                alert("click");
           });

方式二

            //操作元素的属性
             div.onclick = function() {
             alert("onclick");
         }

删除事件

方式一

            div.onclick = null;
            div.onclick = false;
            div.onclick = "";

方式二

            let callback = function() {
                console.log("callback");
            }
            div.addEventListener("click",callback);
            // 删除事件
            div.removeEventListener("click",callback);

分类

  • onsubmit
<form action="aaa" onsubmit="return fun()">


            <input type="text" required>


            <input type="submit" value="提交">


        </form>
        <script>
            function fun() {
                return true;
            }
        </scri

onsubmit事件注意:

1、onsubmit是加在form表单上

2、onsubmit要有return

3、函数也要返回boolea

  • 事件冒泡

子容器还会显示父容器的内容

阻止事件冒泡: 1.在子元素的事件触发函数中阻止

           2.借助event对象

           3.调用一个方法

 <ul onclick="fun()">
            <li>哈哈哈</li>
        </ul>


        <script>
            function fun() {
                alert("111");
            }
            let li = document.querySelector("li");
            li.addEventListener("click",function(event){
                alert("222");
                // 阻止事件冒泡
                event.stopPropagation();
            })

 event对象必须通过addEventListener方式添加事件才可以使用

ES6语法

  • let;const
  • 模板字符串---------着重符``

箭头函数:setInterval(()=>  },2000)

  • Symbol
  • Promise函数,处理回调地狱

###学习心得

今天将JS的基本内容全部学习完毕,感觉掌握情况还可以,基本语句都理解了,还要通过练习巩固

###掌握情况:一般

###课上练习

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            div {
                width: 400px;
                height: 400px;
                border: 1px solid black;
            }
        </style>
    </head>
    <body>

        <a href="demo01.html">再倒计时</a>

        <button onclick="daojishi()">倒计时</button>

        <div></div>

        <script>
            function daojishi() {
                let div = document.querySelector("div");            
                let i = 5;
                let timer = setInterval(function() {
                    div.innerHTML = `<h1>${i}</h1>`;
                    i--;
                    if(i < 0){
                        // clearInterval(timer);
                        
                        div.innerHTML = "<h1>GO!!!</h1>"
                        return;
                    }
                },900);
                // if(i == 0){
                //     clearInterval(timer);
                // }
                console.log(i);
            }
        </script>
    </body>
</html>

###运行结果

 

 

Javascript

自定义属性

获取属性

  • 所有的html元素,我们可以根据具体需求,自定义添加属性
  • 元素.属性名的方式只适用于元素原生的属性

自定义属性

div getAttribute("属性")
  • 设置属性(键值对)
div.setAttribute("属性",“属性名‘)
  • 删除属性
div.removeAttribute("属性")
  • 拿到元素所有属性

返回的结果是一个属性节点的映射的集合

console.log(div.Attributes)
  • 拿到其中某一个节点的子节点
console.log(div.Attributes[index].xxx)

BOM(Browser Object Model)

BoM核心对象-window

  • navigator
  • history
  • screen
  • location

回调函数

回调函数:一个函数的参数是另一个函数

let calllback = function(a{
console.log(a)
}
//传数字
console.log(1)
//传字符串
console.log("hello")
//传对象
console.log({name:"zhangsan"})
//传数组
console.log([1,2,3])

test函数中,入参可以是一个函数

  let test = function(fun){
                console.log("before");
                // 调用了传进来的函数
                fun(1000);
                console.log("after");
            }
            let callback = function(data) {
                console.log("I am callback," + data);
            }
            // test(1)
            test(callback);

传入的参数是一个函数类型时,只需要写函数名,不需要写()

定时函数

  • 定义计时函数

参数1:函数

参数2:延迟时间

  let timer = setTimeout(function(){
                document.write("<h1>5秒钟后可以见到我</h1>")
            },5000);
  • 清除计时函数
 clearTimeout(timer);
  • 周期性定时器
 let timer = setInterval(function() {
                console.log("123");
            },2000);
            clearInterval(timer);

浏览器

  • 浏览器自带了一个小型的数据库

浏览器自带的一个map集合,永久保存

  • 向Storage中设置键值对
  window.localStorage.setItem("name","lucy");
            window.localStorage.setItem("age",25);
  • 从Storage中根据键获取值
 console.log(localStorage.getItem("name"));
  • 从Storage中删除对应的键值对
localStorage.removeItem("name");

从控制台删除此键值对

  • 清空Storage中所有的键值对
 localStorage.clear();

弹窗

  • 警告弹窗
alert(1);
  • 带有确认和取消的弹窗

点击确定,返回true,点击取消,返回false

let flag = confirm("你是好人吗?"); 
 alert(flag);
  • 带有文本框的弹窗

文本框输入的内容就是返回值

 let name = prompt("请输入你的名字:","例如:张三");
   alert(name);

这三个弹窗开发中基本不用!!!

  1. 不好看
  2. 不同浏览器长得不一样
  3. 弹窗位置无法修改

history

历史记录

  function fun() {
                // history.forward();
                // 退 退 退
                 history.back();
                // 既可以前进,又可以后退
                // 传入的参数为正,则前进
                // 传入的参数为负,则后退
                // 参数就是步数,走几步
                history.go(2);
            }

location

获取页面路径

  function fun() {
                // 值是要跳转页面的路径
                // 可以是相对路径,也可以是绝对路径,还可以是http地址
            location.href = "demo02.html";
            location.href = "http://www.baidu.com";
                // 取值
                 alert(location.href);
                // 刷新页面
                location.reload();
           }

screen

           console.log(screen.width);
           console.log(screen.height);
           console.log(screen.availHeight);
           console.log(screen.availWidth);
           

navigater

获取一些浏览器的参数

            console.log(navigator.platform);
            console.log(navigator.appVersion);
            console.log(navigator.appName);

window

    <body>
        <button onclick="fun()">关闭</button>
        <script>
            function fun() {
                // window.close();
                // window.open("demo01.html");
                window.print();
            }
        </script>
    </body>

事件

添加事件

方式一

/ 获取指定的元素
           let div = document.querySelector("div");
           /*
                参数1:要添加的事件类型
                参数2:添加的事件要触发的函数


                传说中,这种添加事件的方式有兼容性问题。
           */
           div.addEventListener("click",function(){
                alert("click");
           });

方式二

            //操作元素的属性
             div.onclick = function() {
             alert("onclick");
         }

删除事件

方式一

            div.onclick = null;
            div.onclick = false;
            div.onclick = "";

方式二

            let callback = function() {
                console.log("callback");
            }
            div.addEventListener("click",callback);
            // 删除事件
            div.removeEventListener("click",callback);

分类

  • onsubmit
<form action="aaa" onsubmit="return fun()">


            <input type="text" required>


            <input type="submit" value="提交">


        </form>
        <script>
            function fun() {
                return true;
            }
        </scri

onsubmit事件注意:

1、onsubmit是加在form表单上

2、onsubmit要有return

3、函数也要返回boolea

  • 事件冒泡

子容器还会显示父容器的内容

阻止事件冒泡: 1.在子元素的事件触发函数中阻止

           2.借助event对象

           3.调用一个方法

 <ul onclick="fun()">
            <li>哈哈哈</li>
        </ul>


        <script>
            function fun() {
                alert("111");
            }
            let li = document.querySelector("li");
            li.addEventListener("click",function(event){
                alert("222");
                // 阻止事件冒泡
                event.stopPropagation();
            })

 event对象必须通过addEventListener方式添加事件才可以使用

ES6语法

  • let;const
  • 模板字符串---------着重符``

箭头函数:setInterval(()=>  },2000)

  • Symbol
  • Promise函数,处理回调地狱

标签:function,25,console,log,08,刘禹彤,let,div,函数
From: https://www.cnblogs.com/lyt0612/p/16625351.html

相关文章

  • 2022-08-22 第二小组 张晟源(JS)
    JSBOM:浏览器对象模型BOM核心对象window回调函数一个函数的参数是另一个函数<script>letcallback=function(a){}//箭头函数......
  • 2022-8-25 剑指offer-字典树-每日一题-二分/排序
    剑指OfferII063.替换单词难度中等25收藏分享切换为英文接收动态反馈在英语中,有一个叫做 词根(root) 的概念,它可以跟着其他一些词组成另一个较长的单词——我......
  • 如何在FirPE中运行AutoHotkey脚本和Maye-Lite-2022年8月25日
     如何在FirPE中运行AutoHotkey脚本和Maye-Lite-2022年8月25日      说明:由于“AutoHotkey中文社区”网站的写文章网页没有实时自动保存当前编辑内容的功能......
  • 202208 网课实录
    前言文化课选手来学文化课了!不知道为啥,上了一半改成了网课,只留下新高一的军训。由于是网课,形式算是特殊的,有记录的必要。之后寒暑假据说有概率继续网课。用的课后网(无......
  • 疑问2022-8-25
    //预处理查询示例funcprepareQueryDemo(){ sqlStr:="selectid,name,agefromuserwhereid>?" stmt,err:=db.Prepare(sqlStr) iferr!=nil{ fmt.Pr......
  • 2022-08-24 第五组 赖哲栋 学习笔记
    JavaScriptJavaScript脚本语言,解释型,主要用来给HTML网页增加动态功能通常的js是运行在浏览器环境下的JS的两种模型DOM:文档对象模型documentBOM:浏览器对象模型wind......
  • 暑假学习三 8.25 继续配置环境
    1.初始化,只能一次,只在第一台机器初始化即可 hdfsnamenode-format需要注意在之前确认没错的情况下执行成功后又以下提示: 2.Hadoop集群的启停止命令shell脚......
  • AT3525 题解
    题目传送门小学生又双叒叕来写题解啦!翻了一下大家的思路,怎么都一样?当数量达到\(10^7\)时,题解代码全爆掉!你问为什么,时间效率\(O(n)\)不稳过吗?对,可是空间复杂度呢,显......
  • AT2586 题解
    题目传送门许多人使用栈,然而根本不需要。先读入整个字符串,然后枚举每个字符。如果当前字符是左括号,往后搜,有就匹配并消除。然而消除这个动作太慢了,如果匹配到,只需把它......
  • P8080 题解
    题目传送门小学生又来写题解啦!你可能会认为,能够使用杯座人数的最大值,就是杯座数量。但结合样例一,若杯座数量大于总人数,只能输出总人数。下一个问题是如何计算杯座数量......