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

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

  • 写回答

19条回答 默认 最新

  • 喵-见缝插针 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';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(18条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题