CSS & jQuery selectors: just a class / id or tag?

When working with class / identifier selectors in CSS and in jQuery, I often see two different approaches:

1. Just a class or id:

CSS

.foo{}

#bar{}

JQuery

$(".foo")

$("#bar")

2. Class or identifier with its tag:

CSS

div.foo{}

div#bar{}

JQuery

$("div.foo")

$("div#bar")

My question is: prohibiting the use of a tag for further refinement of the selector, is there something wrong with placing the tag with the / id class? What is the correct syntax?

I have heard some who say that if a tag is not needed for specificity, it is not like that to put it. While others say it doesn't make any difference, and actually prefers it, as it provides additional information regarding the selector.

What do you think?

+5
6

$("div.foo") div.foo{}

, , . div s.

, , css jQuery.

.

, , , , CSS ( jQuery) ... !

/ /

// , .

@stefmikhail @YoTsumi, :

, , , . , @BoltClock, , .

+5

, (CSS jQuery) (.. ), . jQuery , -, jQuery:

, ".class" , "tag.class", . , , . , 4 30 . ()

+1

jquery - , ( ). jQuery document.getElementById DOM. , , , sizzle , , div.foo , .foo, , DOM. , , ( , ). div.class , .class, -.

+1

Wiley Smashing CSS "

, , CSS . - "" . , , :

  • 0,0,0,1.
  • , 0,0,1,0.
  • 0,1,0,0. ()! . div ul ul li 0,0,0,4 div.aside ul li 0,0,1,3 , a: hover 0,0,1,1 , div.navlinks : 0,0,2,2 , , : hash: title em 0,1,0,1 , h1: hash: title em 0,1,0,2 , , , . , ? "" , , . , , 13 ". , , jQuery . U , , , "active", ( ) , . , ... !
+1
source

Personally, I jump between them like an itchy owl.

Sometimes it's nice to see a tag name to remind you (and your) employees what you actually work with and what behavior you can expect.

I think it’s normal to use.

Go with what feels most natural (doesn't make you hurt your eyes)

0
source

All Articles