Question

I would like to know what is the real differences between all these ways to get an object in jQuery.

  1. $('[id*="partofthename"]')
  2. $('#name')
  3. $find('id_of_control')
  4. $get('id_of_control')

I did some research and I found that:

  1. It's the right way to find a control by its ID, but it's the same as 2 and 4. Am I right?
  2. The second one is a shorcut for number 4. $get. Isn't it?
  3. $find is getting the object with properties and methods
  4. $get is the same as document.getElementById()

Now for number 3. and 4. Big deal. With $find and $get both give me objects. $find is for .NETs findComponent() function

$get & $find are shortcut functions Microsoft has built into their Ajax JavaScript Library.

But when we talk to DOM element and javascript object.

Était-ce utile?

La solution

1 . $('[id*="partofthename"]') selects zero/single/multiple elements id contains partofthename

From Attribute Contains Selector Docs:

Selects elements that have the specified attribute with a value containing the a 
given substring.

2 . $('#name') selects single element with id = name

From ID Selector Docs:

Selects a single element with the given id attribute.

3 . $.find('id_of_control') selects all descendants of element using filter id_of_control

From .find() Docs:

Get the descendants of each element in the current set of matched elements, 
filtered by a selector, jQuery object, or element.

4 . $.get('id_of_control') selects one of the matched elements matched by jQuery object ie., id_of_control

From .get() docs:

Retrieve one of the elements matched by the jQuery object.

Autres conseils

  1. This is the [{attribute_name}{compare}{value}] selector, it select any element that has an attribute {attribute_name} (id) which value {compare} (*= means contains) as of the text value (partofthename) http://api.jquery.com/attribute-contains-selector/
  2. The id selector. http://api.jquery.com/id-selector/
  3. check this post and this one
  4. same
  1. is a slow way to spell 2., and definitely not the right way to get an element by id

  2. is the jQuery way to get an element by id

  3. and 4. are MS-specific APIs in ASP.Net applications, 4. should be roughtly equivalent to 2. (except you don't get a jQuery object, you get a MSAL DomElement object)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top