Paragraph

...">

How to choose a paragraph with only a specific class

Let's say I have HTML code that looks like this:

<p class="p">Paragraph</p> <p class="p p2">Paragraph 2</p> <p class="p p3">Paragraph 3</p> 

If I use: $('.p').css('color','red') , this will apply red color to all paragraphs.

How to apply a style to a paragraph that only has the class p , which is the first paragraph in this case?

+6
source share
4 answers

You can use:

 $('.p[class="p"]').css('color', 'red'); 

or:

 $("p[class='p']").css('color', 'red'); 

Fiddle

+3
source

You can use attribute selector:

 $('p[class="p"]').css('color','red'); 

http://jsfiddle.net/umxGh/

Or:

 $('p').filter(function(){ return this.className === 'p'; }).css('color', 'red'); 
+4
source

Use the attribute selector :

 $('p[class="p"]').css('color','red') 
+3
source

Try the following:

 $('[class=p]').css('color','red'); 
0
source

Source: https://habr.com/ru/post/943820/


All Articles