DOM addressing with jquery
I don't know what it is called but can anyone direct me to a tutorial or something which will enlighten me 开发者_StackOverflow社区on how to address HTML DOM elements in jquery??
For Example, I want to know the difference between $('#someid div')
or $('#someid > div')
.
This is probably the best reference from the jQuery documentation itself: http://api.jquery.com/category/selectors/
jQuery uses CSS selectors for addressing HTML elements. Read the jQuery documentation on its selectors (api.jquery.com/category/selectors) to know the details.
The difference between the selectors you mentioned is following:
#someid div
gets you alldiv
elements located inside element with ID=someid
,#someid > div
gets you alldiv
elements located inside element with ID=someid
, but not enclosed in different elements located in element with ID=someid
- that means that thediv
elements have to be "childs", not just "descendants" of the element with specified ID,
So, the second selector is more specific and the first one is broader.
More on "child selector" is in the documentation.
Selectors are based on CSS selectors so you should get an idea of how they work before you start using them.
$('#someid div') will select the first div within someid that is a direct child, grandchild, etc...
or $('#someid > div') will select only the first child div within the #someid container.
精彩评论