使用jQuery检索和修改元素之前 [英] Retrieve and modify :before element with jQuery
问题描述
我想选择一个由CSS选择器创建的元素:
之前。
I want to select an element which was created by the CSS selector :before
.
我试过了通过使用 $('#element:before')
,但这不起作用,因为它选择了整个元素,而不仅仅是:before
元素。
I tried it by using $('#element:before')
, but that did not work, because it selected the whole element and not only the :before
element.
以下是示例代码: DEMO
Here is the sample code: DEMO
在该例中,只有字符串1.应该是红色的,而不是整个字符串。任何想法如何做到这一点?
In that example, only the string "1. " should be red, not the whole string. Any idea how to do this?
推荐答案
JQuery无法在内容之前设置css属性,因为它不包含在元素中。如果您希望能够在使用javascript处理content之前的颜色,您可以创建一个额外的css类,并添加/删除此类。
JQuery cannot set css properties on :before content, since it is not contained in an element. If you want to be able to manipulate the color of the :before content with javascript, you can create an extra css class, and add/remove this class.
这篇关于使用jQuery检索和修改元素之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!