+ 3
Jquery qiz clarification request
In the qiz of the new Jquery couse, the answer to the question asking how to access the children p elements of the "demo" element is: $("#demo p") But this will select all descendants p elements of the "demo" element. shouldn't the answer be: $("#demo > p") as, this access all CHILDREN elements of "demo" as asked in the qiz???? Oops Spoiler alert....
4 ответов
+ 1
@ Abhilash k. V. il Grandchildren==children there should be no need to differentiate children from descendants: therefore there should be no need for the different sintax
$("demo p") CHILDREN + GRANDCHILDREN
and $("demo > p") CHILDREN
Since this difference exists I don't find your answer correct. Thx anyway for sharing your thoughts
+ 1
The .children() method differs from .find() in that .children() only travels a single level down the DOM tree while .find() can traverse down multiple levels to select descendant elements (grandchildren, etc.) as well.
Based on this Logic Children "p" elements are those next in a single level down the Parent "demo" and they can be accessed via $("demo > p").
Descendants are all children on multiple levels down the Parent "demo" and they can be accessed via $("demo p")
If this is the required answer, the question should relate to the DESCENDANT p element rather than the CHILDREN. It's semantics but it's an educational course
0
$("demo > p") means direct descendants i.e. only direct children. $("demo p") means children + grandchildren +... . Even grandchildren are children in my eyes.
0
In jQuery children involve every dom object inside. Hence for children we use $("demo p"), and when we use $("demo > p") we mean one level descendants.
Besides even grandchildren have children in the name ;).
If u are not satisfied, what should u suggest naming $("demo p") as?