返回顶部

使用JavaScript修改伪类样式的方法

项目中时常会需要用到使用JavaScript来动态控制为元素(:before,:after)的样式,但是我们都知道JavaScript或jQuery并没有伪类选择器。这里总结一下几种常见的方法。

HTML

1
<p class="red">Hi, this is a plain-old, sad-looking paragraph tag.</p>

  


CSS

1
2
3
4
5
6
7
.red::before {
  
content: 'red';
  
color: red;
  
}

  

 

方法一:使用JavaScript或者jQuery切换<p>元素的类名,修改样式。

1
2
3
4
5
6
7
8
9
.green::before {
  
content: 'green';
  
color: green;
  
}
  
$('p').removeClass('red').addClass('green');

  

 

方法二:在已存在的<style>中动态插入新样式。 

1
2
3
#我使用的是这个方法去向某个伪类改变的样式,其他的都没试的哈,大家可试试的,有结果了告诉博主一声哦document.styleSheets[0].addRule('.red::before','color: green');
  
document.styleSheets[0].insertRule('.red::before { color: green }', 0);-----------------------------------------------------------------------------------------------------------------------------------------------------自己的代码检验HTML<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>23、提交按钮加载动画效果 </title>    <link rel="stylesheet" href="../css/font-awesome-4.7.0/css/font-awesome.min.css">    <style>    /*将内外边距清除掉*/    *{        padding: 0;        margin: 0;    }    body{        width: 100vw;        height: 100vh;        display: grid;        display: grid;        grid-template: 1fr/1fr    }    button{        width: 200px;        height: 50px;        background: #ecf0f1;        color:#34495e ;        border: solid 1px #ddd;        /*下面的这两句话是使得grid(栅格化布局)中的文本水平居中+垂直居中*/        justify-self: center;        align-self: center;    }    button::after{        content: '';        width: 3px;        height: 3px;        /*使得制作影子的这块块级元素变为行内元素*/        display: inline-block;       animation-name: none;   /*!!!!!!! animation-name: hd;这个代码可以通过js控制,当点击的时候加上这个动画属性,当不点击的时候就去掉这个动画name*/        animation-duration: 1s;        animation-iteration-count: infinite;    }    @keyframes hd {        from{            box-shadow: none;        }        30%{            box-shadow: 4px 0 0  currentColor;        }        60%{            box-shadow: 4px 0 0 currentColor,11px 0 0 currentColor;        }        90%{            box-shadow: 4px 0 0 currentColor,11px 0 0 currentColor,18px 0 0 currentColor;        }    }    </style>    <!--animation---></head><body><!--<div></div>--><button class="bu">动态按钮</button></body><script src="../../MyEcharts/js/jquery.js"></script><script type="text/javascript">    $(".bu").click(function (){       // alert('00');            document.styleSheets[0].addRule('.bu::after','animation-name:hd');       document.styleSheets[0].insertRule('.bu::after{animation-name:hd}',0);    })</script></html>

  

 

 

方法三:创建一份新的样式表,并使用JavaScript或jQuery将其插入到<head>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Create a new style tag
  
var style = document.createElement("style");
  
// Append the style tag to head
  
document.head.appendChild(style);
   
// Grab the stylesheet object
  
sheet = style.sheet
   
// Use addRule or insertRule to inject styles
  
sheet.addRule('.red::before','color: green');
  
sheet.insertRule('.red::before { color: green }', 0);

  

jquery:

1
$('<style>.red::before{color:green}</style>').appendTo('head');

  

方法四:使用HTML5的data-属性,在属性中使用attr()动态修改。

 

1
2
3
4
5
6
7
8
9
10
11
<p class="red" data-attr="red">Hi, this is plain-old, sad-looking paragraph tag.</p>
  
.red::before {
  
content: attr(data-attr);
  
color: red;
  
}
  
$('.red').attr('data-attr', 'green');

  

自己测试过这些方法,都可以解决修改伪类样式,但是都存在局限性,希望给读者能提出更好的解决方法,希望前端能够越来越强大

posted @   fen斗  阅读(1932)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
历史上的今天:
2019-08-27 LINUX 怎么实现root和普通用户的切换及怎么更改root密码
2019-08-27 mount命令实际操作样例
2019-08-27 Mount命令的参数详解
2019-08-27 Linux挂载(mount,umount及开机自动挂载
2019-08-27 什么是挂载?mount的用处在哪?
点击右上角即可分享
微信分享提示