您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

如何利用JS对CSS样式进行更改

2024/5/1 21:26:29发布13次查看
本文主要描述了,利用javascript对css样式中的行内样式和类样式进行更改,简化日常的操作流程,让html界面更加简洁。
html中的内容:
<!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>css操作</title> <style> .bgn-cyan{ background-color: cyan; } .bgn-yellow{ background-color: #ff0; } .border{ border:3px solid #000; } .bolder{ font-weight: bolder; } p{ height: 25px; } </style></head><body></body></html>
1.行内样式
<script> //获取p标签元素 const p=document.queryselector("p"); //验证是否获取成功 console.log(p); p.style.color="red"; </script>
2.类样式
<script> const p=document.queryselector("p"); //添加类样式 p.classlist.add("bgn-cyan"); //添加多个类样式 p.classlist.add("border","bolder"); //对于样式的移除 p.classlist.remove("bolder","border"); //对样式的替换 p.classlist.replace("bgn-cyan","bgn-yellow"); //对是否有这个样式进行取反 p.classlist.toggle("bgn-cyan"); //原有样式("bgn-cyan")被替换过后replace("bgn-cyan","bgn-yellow") 使用toggle("bgn-cyan")无法实现</script>
推荐:《2021年js面试题及答案(大汇总)》
以上就是如何利用js对css样式进行更改的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product