AX0101_4413 2021-11-07 09:50 采纳率: 95.7%
浏览 38
已结题

如何用js操作CSS伪类?

这是我的代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .A:focus-visible {
            outline: none;
        }
    </style>
</head>
<body>
<div contentEditable="true" class="A"></div>
</body>
</html>

这是用CSS实现的。我希望用JS实现。

  • 写回答

3条回答 默认 最新

  • CSDN专家-showbo 2021-11-07 14:30
    关注

    测试代码如下

    <!doctype html>
    <title>javascipt修改css级联样式表类和伪类样式</title>
    <style>
    .exampleA{color:Blue}
    .exampleA:before {content:"-";color:red;font-size:20px;font-weight:bold}
    </style><!--IE条件注释,判断是否IE8浏览器-->
    <!--[if IE 8]><script>var IE8=true</script><![endif]-->
    <script>
        function changecss(theClass, element, value) {
            var sl = document.styleSheets.length;
            if (sl == 0) return;
            var isPseudo = theClass.indexOf(':') != -1;//css伪类
            //非IE8-,js获取到伪类名称包含二个冒号
            if (window.IE8) theClass = theClass.replace(/:+/g, ':');
            else theClass = theClass.replace(/:+/g, '::');
            var cssRules = document.styleSheets[0].rules ? 'rules' : 'cssRules';
            for (var i = 0; i < sl; i++) {
                for (var j = 0, rl = document.styleSheets[i][cssRules].length; j < rl; j++) {
                    if (document.styleSheets[i][cssRules][j].selectorText == theClass) {
                        if (isPseudo) //css伪类需要覆盖,替换值没用
                            document.styleSheets[i].addRule ?
                                document.styleSheets[i].addRule(theClass, element + ':' + value) ://IE8+,chrome
                                document.styleSheets[i].insertRule(theClass + '{' + element + ':' + value + '}', rl);//firefox
                        else document.styleSheets[i][cssRules][j].style[element] = value;
                        break;
                    }
                }
            }
        }
     </script>
    
    <span class="exampleA">Example A</span><br />
    <span class="exampleB">Example B</span><br />
    <span class="exampleA">Example A</span><br />
    <input type="button" value="修改exampleA为红色" onclick="changecss('.exampleA','color','red')"/>
    <input type="button" value="修改exampleA为黑色" onclick="changecss('.exampleA','color','black')" />
    <input type="button" value="修改exampleA:before伪类color为blue" onclick="changecss('.exampleA::before','color','blue')" />
    <input type="button" value="修改exampleA:before伪类color为red" onclick="changecss('.exampleA::before','color','red')" />
    

    来源:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 11月15日
  • 已采纳回答 11月7日
  • 创建了问题 11月7日

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示