What is the difference between class and id in jQuery?

What is the difference between class and id in jQuery? For instance:

<span class="lalal"></span>

and

<span id="lalal"></span>

Because one works well with jQuery and the other does not. Thank.

+3
source share
5 answers

The identifier must be unique on the page, when you have several elements with the same identifiers, jQuery selects only the first. This is because he does not need to worry about finding others, since there should not be more. which may explain the strange behavior you experience.

, , . , . jQuery, HTML CSS .

+7

, jQuery - jQuery "" HTML/CSS.

: , .

, .

+5

CSS. HTML- (, "mainmenu" ), . , .

, , "" , :

$('a.green').hide();

"" . ( < div id = "mainmenu" > ), :

$('#mainmenu').hide();
+4

, , . ( , jQuery ).

:

<span class="myclass myclass2 mycall3"></span>

<span class="myclass"></span>

<span id="myspan"></span>

, .

:

<span id="myspan">Hello</span>

<span id="myspan">Goodbye</span>

, jQuery

 $("#myspan").html();

jQuery Hello

Side note: classes and identifiers have nothing to do with jQuery and everything related to valid HTML. jQuery and CSS use these conventions to select specific elements.

+1
source

An identifier uniquely identifies an element on a web page regardless of its type (button, div, radio, etc.) when the class is used to identify a specific type of element.

amuses

0
source

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


All Articles