Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS pseudo-code is not recognised by jQuery

Tags:

jquery

css

my CSS pseudo-code is not recognised by jQuery: here's my code

css:

h1 {
    background: red;
    width: 100px;
    display: inline-block;
}

h1:after {
    content:" | ";
    background:blue;
    display: inline-block;
}

then in jQuery i do:

console.log($('h1').css('backgroundColor'));

is shows:

rgb(255, 0, 0)

but when i do:

console.log($('h1:after').css('backgroundColor'));

is shows:

undefined

like image 657
rcs20 Avatar asked Feb 14 '12 17:02

rcs20


People also ask

How to use after jQuery?

The after() is an inbuilt function in jQuery which is used to insert content, specified by the parameter for the each selected element in the set of matched element. Syntax: $(selector). after(A);

How do I target a pseudo element in jQuery?

You can't. A pseudo element is not in the DOM so can't be selected by JS.

Can you select pseudo elements with JavaScript?

Since they're not part of the DOM, CSS pseudo-elements can't be selected and edited with JavaScript the same way as traditional elements on a page.

How to add div after div in jQuery?

jQuery insertAfter() Method The insertAfter() method inserts HTML elements after the selected elements. Tip: To insert HTML elements before the selected elements, use the insertBefore() method.


1 Answers

You cannot select css pseudo-elements because they don't actually exist in the DOM

like image 156
Didier Ghys Avatar answered Oct 27 '22 12:10

Didier Ghys