Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

javascript - 使用jQuery选择和操作CSS伪元素,例如:: before和:: after(Selecting and manipulating CSS pseudo-elements such as ::before and ::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?

(有没有办法使用jQuery选择/处理CSS伪元素,例如::before::after (以及带有一个分号的旧版本)?)

For example, my stylesheet has the following rule:

(例如,我的样式表具有以下规则:)

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

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

(如何使用jQuery将'foo'更改为'bar'?)

  ask by JBradwell translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

(您还可以将内容传递给具有data属性的伪元素,然后使用jQuery进行操作:)

In HTML:

(在HTML中:)

<span>foo</span>

In jQuery:

(在jQuery中:)

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

In CSS:

(在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:

(如果要防止显示“其他文本”,可以将其与seucolega的解决方案结合使用,如下所示:)

In HTML:

(在HTML中:)

<span>foo</span>

In jQuery:

(在jQuery中:)

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

In CSS:

(在CSS中:)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...