jQuery和伪元素 [英] jQuery and pseudo-elements
问题描述
我动态地改变了在CSS中用定义的en元素的位置:在
之后。
我试图通过这种方式改变它:
I have dynamically change position of en element which defined in CSS with :after
.
I tried to change it this way:
$(function(){
$('div::after').css({'top':'20px'})
})
但它不起作用。有没有办法改变立场?
But it doesn't work. Is there any ways to change position?
推荐答案
你不能。由创建的内容:在
或之后:
之前不是DOM的一部分,因此无法选择或修改。
You can't. Content created by :after
or :before
is not part of the DOM and therefore cannot be selected or modified.
如果你看看这个示例小提琴并检查DOM中的Firebug或类似的东西你会看到DOM树中没有伪元素。
If you have a look at this example fiddle and inspect the DOM in Firebug or similar you will see that the pseudo-element is not present in the DOM tree.
一个潜在的解决方案是将一个类应用于你想要改变的元素,并在CSS中适当地设置该类的样式:
A potential solution would be to apply a class to the element you want to change, and to style that class appropriately in CSS:
$("div").addClass("newClass");
请参阅这小提示一个例子。
这篇关于jQuery和伪元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!