七度&光 2011-02-18 12:53 采纳率: 22.2%
浏览 555
已采纳

选择和操作 CSS 伪元素,例如: : before 和: : after using jQuery

Is there any way to select/manipulate CSS pseudo-elements such as ::before and ::after (and the old version with one semi-colon) using jQuery?

For example, my stylesheet has the following rule:

.span::after{ content:'foo' }

How can I change 'foo' to 'bar' using jQuery?

转载于:https://stackoverflow.com/questions/5041494/selecting-and-manipulating-css-pseudo-elements-such-as-before-and-after-usin

  • 写回答

21条回答 默认 最新

  • perhaps? 2011-04-20 17:59
    关注

    You could also pass the content to the pseudo element with a data attribute and then use jQuery to manipulate that:

    In HTML:

    <span>foo</span>
    

    In jQuery:

    $('span').hover(function(){
        $(this).attr('data-content','bar');
    });
    

    In CSS:

    span:after {
        content: attr(data-content) ' any other text you may want';
    }
    

    If you want to prevent the 'other text' from showing up, you could combine this with seucolega's solution like this:

    In HTML:

    <span>foo</span>
    

    In jQuery:

    $('span').hover(function(){
        $(this).addClass('change').attr('data-content','bar');
    });
    

    In CSS:

    span.change:after {
        content: attr(data-content) ' any other text you may want';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(20条)

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料