derek5. 2011-02-18 12:53 采纳率: 100%
浏览 539
已采纳

选择和操作 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

  • 写回答

20条回答 默认 最新

  • 谁还没个明天 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';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(19条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大