JavaScript学习笔记(5.0)

32 篇文章 0 订阅
13 篇文章 0 订阅

渲染页面示例

<!--
 * @Author: RealRoad1083425287@qq.com
 * @Date: 2023-03-12 09:12:54
 * @LastEditors: Mei
 * @LastEditTime: 2023-03-12 09:31:57
 * @FilePath: \vscode\渲染页面.html
 * @Description: 
 * 
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
-->
<!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>
        *{
            margin: 0;
            padding: 0;
        }
        ul{
            list-style: none;
        }
        li{
            overflow: hidden;
        }
        li img{
            float: left;
            width: 100px;
        }
    </style>
</head>
<body>
    <ul id="movie">
        <li>
            <img src="img/0120.jpg" alt="">
            <h3>标题1</h3>
            <p>文本1</p>
        </li>
    </ul>
    <script>
        var filmlist=[
            {
                url:"img/0120.jpg",
                title:"测试标题1",
                grade:2.3
            },
            {
                url:"img/0121.jpg",
                title:"测试标题2",
                grade:2.5
            },
            {
                url:"img/0123.jpg",
                title:"测试标题3",
                grade:2.9
            }
        ]
        var filmitem=filmlist.map(function(item){
            return `<li>
            <img src="${item.url}" alt="">
            <h3>${item.title}</h3>
            <p>${item.grade}</p>
        </li>`
        })
        // console.log(filmitem.join(""))
        var test=document.querySelector("#movie")
        test.innerHTML=filmitem.join("")
    </script>
</body>
</html>

 操作元素样式【行内】(可以进行读写操作)

box.style.height               //只能获取行内样式(style)
//访问行内样式   background-color
box.style["background-color"]
//或
box.style.backgroundColor

对于所有样式的万能方法(但是不能给样式赋值)

getComputedStyle(box) .width          //.backgroundColor   //["background-color"]

该方法不支持IE(6,7,8)低版本浏览器,对应的IE浏览器使用obox.currentStyle.backgroundColor

操作元素类名

.className
//删除类名,直接进行赋值
box.className=""
//增加类名
box.className+=" item3"  //前面必须有空格,否则就会对当前类名的最后一个类名进行拼接
//增加
box.classList.add("item3")

//删除
box.classList.remove("item3") //一个一个删除
//切换  (如果有这个类就删除,没有就增加)
box.classList.toggle("item")

选项卡示例

<!--
 * @Author: RealRoad1083425287@qq.com
 * @Date: 2023-03-12 10:07:07
 * @LastEditors: Mei
 * @LastEditTime: 2023-03-12 11:03:42
 * @FilePath: \vscode\选项卡.html
 * @Description: 
 * 
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
-->
<!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>
        *{
            margin: 0;
            padding: 0;
        }
        ul{
            /* display: flex; */
            list-style: none;
        }
        li{
            height: 50px;
            line-height: 50px;
            text-align:center ;
            flex: 1;
        }
        
        .header{
            display: flex;
            width: 500px;
        }
        .header li{
            flex:1;
            height: 50px;
            line-height: 50px;
            text-align: center;
            border: 1px solid black;
        }
        .box{
            position: relative;
        }
        .box li{
            position: absolute;
            left: 0;
            top:0;
            width: 500px;
            height: 200px;
            background-color: yellow;
            display: none;
        }
        .header .active{
            background-color: red;
            /* border-bottom: 1px solid red; */
        }
        .box .active{
            display: block;
            /* border-bottom: 1px solid red; */
        }
    </style>
</head>
<body>
    <ul class="header">
        <li class="active" >正在上映</li>
        <li >往期作品</li>
        <li >往期作品</li>
        <li >往期作品</li>
    </ul>
    <ul class="box">
        <li class="active" >正在上映</li>
        <li >往期作品</li>
        <li >往期作品</li>
        <li >往期作品</li>
    </ul>
    <script>
        var oHeaderItems=document.querySelectorAll(".header li")
        var oBoxItems=document.querySelectorAll(".box li")

        for(var i=0; i<oHeaderItems.length; i++){
            //自定义属性
            oHeaderItems[i].dataset.index=i
            oHeaderItems[i].onclick=handler
        }
        function handler(){
                // console.log(111)
                // console.log(this.dataset.index)
                var index=this.dataset.index
                for(var m=0;m<oHeaderItems.length; m++){
                    oHeaderItems[m].classList.remove("active")
                    oBoxItems[m].classList.remove("active")
                }
                oHeaderItems[index].classList.add("active")
                oBoxItems[index].classList.add("active")
            }
        
        
    </script>
</body>
</html>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mez_Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值