web12.12-14笔记

函数

立即执行函数

组成:

l 定义一个函数

l 将整个函数包裹在一对括号中

l 将函数声明转换为表达式

l 在结尾加上一对括号

l 让函数立即被执行

多个立即执行函数之间用; 隔开

 <script>
        (function () { console.log("666") })();

        (function () { console.log("777") }());
 </script>

闭包

函数里面的变量外面不能访问

闭包可以使外界获取局部变量的值,可以让一个变量一直驻留在内存当中,缺点是内存无法释放

 <script>
        function outer() {
            let money = 5000
            function inner() {
                console.log(money)
            }
            return inner
        }


        let fn = outer()
        fn()
</script>

 json对象

json对象(无顺序):把无数个繁杂的属性封装成一个整体的容器

json对象的定义:键值对的形式(key:value)

json对象的键必须带双引号

 <script>
        //let uname = "iKun"
        //let age = 23
        //let score = 100
        //function sing() {
            //console.log("sing~~~")
        //}
        //function rap() {
            //console.log("rap~~")
        //}
        
        let stu = {
            "uname": 'ikun',
            age: 23,
            "score": 100,
            "sing": function () {
                console.log("sing~~~")

            },
            "rap": function () {
                console.log("rap~`~~~")
            }
        } 
         console.log(stu)

读取对象属性

分量运算符 对象名. 属性名

console.log(stu.uname)
stu.rap()

下标法     对象名【索引的值】

console.log(stu["age"])
stu["rap"]()

添加 对象名. 新的属性名 = 属性值

新的代表添加,没有则为重新赋值

stu.gender = "M"
console.log(stu)

stu.uname = "zhangsan"
console.log(stu)

json的遍历     for in

结合下标法

for (let i in stu) {
     console.log(i)
     console.log(stu[i])
}

json进阶

    <script>
        let data = {
            "info": "学生的三门成绩",
            "score": [
                {
                    "kemu": "语文",
                    "score": 77
                },
                {
                    "kemu": "数学",
                    "score": 21
                },
                {
                    "kemu": "英语",
                    "score": 33
                }
            ]
        }
        console.log(data.score[1].kemu)
    </script>

定时器    setTimeout()  setInterval()

setTimeout():让某一个函数或者某一段代码(加'')在多少毫秒之后执行,并返回一个整数,作为定时器的编号

setTimeout(func|code,delay)

// setTimeout(() => {
//     console.log("66666666")
// }, 2000)


 function fn() {
     console.log("666")
 }

 setTimeout(fn, 2000)
 
setTimeout('console.log("666")', 2000)
setTimeout((a, b) => {
     console.log(a + b)
 }, 2000, 2, 5)   //2,5会在2000毫秒之后函数执行时,作为参数传递给函数的形参

this

——谁调用指向谁

 let obj = {
     x: 2,
     y: 4,
     fn: function () {
         console.log(this.x)
         console.log(this)
     }
 }

// obj.fn()
 let tim = window.setTimeout(function () {
     obj.fn()
 }, 2000)
 console.log(tim)

(window可省略)

bind

——绑定this的指向

 let timer = setTimeout(obj.fn.bind(obj), 2000)
 console.log(timer)

clearTimeout(tim)     通过定时器编号,清除定时器(clear+ 定时器编号 )

        let timer = setInterval(() => {
            console.log("6666")
            console.log("77777")

        }, 1000)


        clearInterval(timer)

随机数

    <script>
        console.log(Math.random())
        // 0-10之间的整数
        console.log(Math.round(Math.random() * 10))
        // 1-6之间的整数
        Math.round(Math.random() * 5) + 1


        // min, max
        function getRandom(min, max) {

            if (max > min) {
                return Math.round(Math.random() * (max - min) + min)

            } else {
                return "你疯了把"
            }

        }
        console.log(getRandom(10, 20))
    </script>

获取元素对象

<body>
    <div class="box">你是一个盒子</div>

    <ul>
        <li>11</li>
        <li>21</li>
        <li id="li3">31</li>
        <li>41</li>
        <li>51</li>
    </ul>

    <script>
        // 获取元素   通过css选择器获取元素对象
        const box = document.querySelector(".box")
        console.log(box)
        console.dir(box)


        const li3 = document.querySelector("ul li:nth-child(3)")
        console.log(li3)
        const li = document.querySelector("ul li")
        console.log(li)


        const lis = document.querySelectorAll("ul li")
        console.log(lis)

        for (let i = 0; i < lis.length; i++) {
            lis[i].style.color = "pink"

        }


        console.log(document.getElementById("li3"))
        console.log(document.getElementsByClassName("box"))
        console.log(document.getElementsByTagName("li"))

    </script>

</body>

元素对象内容

<body>
    <div class="box"></div>
    <script>
        // 1、获取元素对象
        let box = document.querySelector(".box")
        // console.log(box)
        // 2、更改
        box.innerText = `<h1>你是一个盒子</h1>`
        box.innerHTML = `<h1>你是一个盒子</h1>`
    </script>
</body>

更改元素对象的属性

<body>
    <img src="../images/1.jpg" alt="" title="">
    <input type="password" name="" id="">

    <button disabled>点击</button>

    <script>
        // 对象.属性=值
        // 1、获取元素
        let img = document.querySelector("img")
        let ipt = document.querySelector("input")

        let btn = document.querySelector("button")
        // console.log(img)

        // 2、更改src属性
        img.src = "../images/2.jpg"
        img.title = "你是一个好人"

        ipt.type = "text"

        btn.disabled = false

    </script>
</body>

元素属性

普通属性

——不在style标签里面的

元素对象.属性名= 属性值

let img = document.querySelector("img")
img.src = "../images/2.jpg"
 // 
console.log(img.getAttribute("src"))
img.setAttribute("title", "你是一个大漂亮")

style样式属性

元素对象.style.属性名 = "属性值"
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            display: none;
            width: 300px;
            height: 300px;
            background-color: pink;
        }

        .new {
            display: block;
            width: 1200px;
            height: 300px;
            background-color: greenyellow;
        }
    </style>
</head>

<body>
    <div class="box">nckcdcjd</div>

    <script>
        let box = document.querySelector(".box")
        box.style.width = "500px"
        box.style.height = "600px"
        box.style.backgroundColor = "red"
        box.style.display = "block"
    </script>

</body>

</html>
className     会覆盖掉原来的类名
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            display: none;
            width: 300px;
            height: 300px;
            background-color: pink;
        }

        .new {
            display: block;
            width: 1200px;
            height: 300px;
            background-color: greenyellow;
        }
    </style>
</head>

<body>
    <div class="box">nckcdcjd</div>

    <script>
        let box = document.querySelector(".box")
        box.className = "box new"
    </script>

</body>

</html>
classList     获取style样式属性的属性值
box.classList.add("new")
box.classList.remove("box")
box.classList.toggle("box")  //切换

box.classList.toggle("box")——有box类名就删除box类名,无就追加

offset属性

——只读属性,只能查看,不能赋值

offsetTop 返回元素相对带有定位父亲元素上方的偏移

offsetLeft 返回元素相对带有定位父亲元素左边的偏移

offsetWidth 返回自身包括padding,边框,内容区的宽度,返回数不带单位

offsetHeight 返回自身包括padding,边框,内容区的高度,返回数不带单位

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            position: absolute;
            top: 200px;
            left: 100px;
            background-color: aqua;
            width: 300px;
            height: 300px;
        }
    </style>
</head>

<body>
    <div class="box"></div>
    <script>
        let box = document.querySelector(".box")
        console.log(box.offsetWidth)
        console.log(box.offsetHeight)
        console.log(box.offsetTop)
        console.log(box.offsetLeft)
    </script>
</body>

</html>

随机图片

<body>
    <img src="../images/1.jpg" alt="">
    <script>

        let img = document.querySelector('image')
        let srcs = ["../images/1.jpg", "../images/2.jpg"]
        function getRandom(min, max) {

            if (max > min) {
                return Math.round(Math.random() * (max - min) + min)

            } else {
                return "你疯了把"
            }

        }

        let src = getRandom(0, srcs.length - 1)
        console.log(src)

    </script>
</body>

节点的相关操作

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <ul>

    </ul>

    <script>
        // 1、创建一个li标签
        let newLi = document.createElement("li")
        // 2、添加内容
        newLi.innerHTML = `<a href="#">我是新添加的li</a>`
        console.log(newLi)
        // 元素的追加   父元素
        let ul = document.querySelector("ul")
        ul.appendChild(newLi)

    </script>

</body>

</html>

查找节点

通过父亲获取子节点

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="father">
        父亲
        <div class="son">儿砸1</div>
        <div class="son">儿砸2</div>
        <div class="son">儿砸3</div>
        <div class="son">儿砸4</div>
    </div>
    <script>
        let f = document.querySelector(".father")
        console.log(f.children[2])
        console.log(f.childNodes)
    </script>

</body>

</html>

通过儿子找到父节点

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="father">
        父亲
        <div class="son">儿砸1</div>
        <div class="son">儿砸2</div>
        <div class="son">儿砸3</div>
        <div class="son">儿砸4</div>
    </div>
    <script>
        let s = document.querySelector(".son")
        console.log(s.parentNode)
    </script>

</body>

</html>

兄弟节点

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="father">
        父亲
        <div class="son">儿砸1</div>
        <div class="son">儿砸2</div>
        <div class="son2">儿砸3</div>
        <div class="son">儿砸4</div>


    </div>
    <script>
        let son2 = document.querySelector(".son2")
        console.log(son2.previousElementSibling)
        console.log(son2.nextElementSibling)
    </script>

</body>

</html>

inserBefore

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>

    </ul>

    <script>
        let ul = document.querySelector("ul")
        // 创建li
        let newLi = document.createElement("li")
        // 添加内容
        newLi.innerHTML = `新来的`
        // 3、追加
        ul.appendChild(newLi)

        // ul.insertBefore(目标节点,参照节点)
        ul.insertBefore(newLi, ul.children[0])

    </script>

</body>

</html>

克隆节点

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="father">
        xsxs
        <div class="son">erza</div>
    </div>

    <script>
        let newf = document.querySelector(".father")
        console.log(newf.cloneNode(true))
    </script>

</body>

</html>

删除节点

——基于父元素进行

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>

    </ul>

    <script>
        let ul = document.querySelector("ul")
        ul.removeChild(ul.children[1])
    </script>

</body>

</html>

事件监听

事件:事件源(触发事件的东西)、事件类型(键盘的按下,鼠标的点击)、事件对象

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .pink {
            background-color: pink;
        }
    </style>
</head>

<body>
    <button>点击</button>
    <textarea name="" id="" cols="30" rows="10"></textarea>
    <script>
        let txt = document.querySelector("textarea")

        txt.addEventListener("mouseenter", () => {
            txt.style.backgroundColor = "pink"
        })
        txt.addEventListener("mouseleave", () => {
            txt.style.backgroundColor = "red"
        })

    </script>

</body>

</html>

事件源.addEventListener("事件类型",处理函数,true|false)

let btn = document.querySelector("button")
btn.addEventListener("click", () => {
    alert("666")
})

lv0      事件源.on事件类型=处理函数

btn.onclick = function () {
    alert("666")
}

test

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .pink {
            background-color: pink;
        }
    </style>
</head>

<body>
    <button>111</button>
    <button>222</button>
    <button>333</button>
    <button>444</button>
    <button>555</button>
    <script>
        // 获取元素
        let btns = document.querySelectorAll("button")
        console.log(btns)

        // 添加事件
        for (let i = 0; i < btns.length; i++) {
            btns[i].addEventListener("mouseenter", function () {
                for (let j = 0; j < btns.length; j++) {
                    btns[j].classList.remove("pink")
                }
                btns[i].classList.add("pink")

            })

        }
    </script>

</body>

</html>
  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值