JQuery: What is the difference between an element reference using # [objectId] or [id = objectId]

Can someone tell me what is the difference between linking to an element using #[objectId]or [id=objectId]?

+4
source share
1 answer

The first is very fast, as jQuery internally uses getElementByIdwhen it recognizes a pattern (using a regular expression).

The second queries jQuery to iterate over all objects with an identifier. It is very slow. jQuery doesn't even stop the iteration when it finds one match in this case.

[id... , , , , "something", $('[id^=something]').

, HTML (no reused id) , $('#'+someId) ( -, Sizzle). $(document.getElementById(someId)).


: , "@" ID Sizzle ( jQuery) . Sizzle :

rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/,

/^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/.test('#som@thing') false.

+8
source

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


All Articles