使用`.css'作为伪元素 [英] Using `.css` for pseudo-elements

查看:115
本文介绍了使用`.css'作为伪元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

操作CSS:before和:after伪元素使用jQuery

我想使用jQuery等效这个CSS代码:

I would like to the jQuery equivalent this CSS code:

p:before {
   content: 'Intro!';
}



我初次尝试过 $('p: before')。css('content','Intro!'); 但它不工作。

-element使用jQuery的CSS修改?

How can I do pseudo-element CSS modifications using jQuery?

推荐答案

你不能。

1)选择器 p:before 不仅仅是一个选择器 - 不影响实际选择。什么是jQuery应该返回,所有 p 标签?这是 $(...)所有 - 它返回一堆与你的选择器匹配的元素。 jQuery(Sizzle / querySelectorAll )不知道如何获取:之前 ...

1) The selector, p:before, is not just a selector -- the psuedo element defines functionality that does not affect the actual selection. What is jQuery supposed to return, all p tags? That's all $(...) does -- it returns a bunch of elements that match your selector. jQuery (Sizzle/querySelectorAll) doesn't know how to fetch :before...

2)Psuedo-element /类的行为不能通过JS API使用。

2) Psuedo-element/class behaviour is not available via JS APIs either.

这样做的唯一方法是动态通过JS创建一个< style> 元素,如下所示:

The only way to do something like this, dynamically via JS, would be to create a <style> element, like so:

$('<style>p:before{content:"intro";}</style>').appendTo('head');

我想问你为什么要这样做?

May I ask why you want to do this in the first place?

这篇关于使用`.css'作为伪元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆