js添加css样式的另一种方式-创建样式规则【2021.12.10】

1.利用document.createElement("style")创建style标签

2.利用innerHtml加入样式

3.利用document.head.appendChild(style)将style元素放入head标签里面

例子:

<!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>
        .contenter>*{
            width: 80px;
            height: 80px;
            text-align: center;
            line-height: 80px;
            font-size: 22px;
        }
        .contenter div:nth-child(1){
            background-color: rgb(238, 96, 96);
        }
        .contenter div:nth-child(2){
            background-color: rgb(121, 247, 109);
        }
    </style>
</head>
<body>
    <div class="contenter">
    <div>1</div>
    <div>2</div>
    </div>
</body>
<script>
    const divs = document.querySelectorAll(".contenter div");
    let contentStyle = document.createElement("style");
    // 创建的的元素并不属于document对象,只是创建出来,并未添加到HTML文档中。添加的方式有appendChild或者insertBefore方法。
        contentStyle.innerHTML=`.contenter{
            width:180px;
            heifht:180px;
            background:black;
            }`
            console.log(contentStyle)//style
            console.log(contentStyle.nodeType)//1,为元素节点
            // 可以看出元素节点包含了属性节点
    for(let i =0;i<divs.length;i++){
        const contenter = document.querySelector(".contenter");
       divs[i].addEventListener("click",()=>{
           if(i==0){
               document.head.appendChild(contentStyle)
           }else{
               document.head.removeChild(contentStyle)
           }
       })
            
    }
</script>
</html>

最后简单说一下什么是元素节点:

每个HTML标签都是一个元素节点

而文本节点就是包含在元素节点中的文本,空白也算哦

属性节点是每一个HTML属性

注释属于注释节点

整个文档是一个文档节点

元素节点和文本节点属于父子级关系

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的样式表文件“style.css”的示例代码,您可以根据需要进行修改和完善。 ```css /* 全局样式 */ body { font-family: Arial, sans-serif; font-size: 16px; line-height: 1.5; margin: 0; padding: 0; } header { background-color: #333; color: #fff; } nav ul { list-style: none; margin: 0; padding: 0; display: flex; justify-content: space-around; align-items: center; height: 80px; } nav a { color: #fff; text-decoration: none; font-size: 20px; } nav a:hover { color: #ff0; } /* 首页样式 */ .intro { display: flex; flex-direction: column; align-items: center; margin-top: 50px; } .intro img { border-radius: 50%; width: 200px; height: 200px; object-fit: cover; object-position: center; margin-bottom: 20px; } .intro h1 { font-size: 36px; margin: 0; } .intro p { font-size: 20px; margin: 0; } /* 关于我样式 */ .aboutme { padding: 50px; } .aboutme h2 { font-size: 36px; margin-bottom: 20px; } .aboutme p { font-size: 20px; margin-bottom: 10px; } /* 作品集样式 */ .portfolio { padding: 50px; } .portfolio h2 { font-size: 36px; margin-bottom: 20px; } .portfolio ul { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; justify-content: space-between; } .portfolio li { margin-bottom: 30px; width: 30%; } .portfolio img { width: 100%; object-fit: cover; object-position: center; margin-bottom: 10px; } .portfolio p { font-size: 18px; margin: 0; } /* 博客样式 */ .blog { padding: 50px; } .blog h2 { font-size: 36px; margin-bottom: 20px; } .blog ul { list-style: none; margin: 0; padding: 0; } .blog li { margin-bottom: 30px; } .blog h3 { font-size: 24px; margin-bottom: 10px; } .blog p { font-size: 18px; margin: 0; } /* 联系我样式 */ .contact { padding: 50px; } .contact h2 { font-size: 36px; margin-bottom: 20px; } .contact form { display: flex; flex-direction: column; } .contact label { font-size: 20px; margin-bottom: 10px; } .contact input, .contact textarea { font-size: 16px; padding: 10px; margin-bottom: 20px; } .contact input[type="submit"] { background-color: #333; color: #fff; cursor: pointer; } ``` 上面的代码中定义了一些全局样式,以及首页、关于我、作品集、博客、联系我等不同部分的样式规则。您可以根据需要进行修改和完善。同时,需要注意的是,样式表文件“style.css”需要与HTML文件放在同一个目录下。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值