Home >Web Front-end >JS Tutorial >Summary of three common mistakes in calling parentsUntil in Jquery
The parentsUntil() method of
Jquery is used to find the ancestors of Jquery object . There are three issues that you must pay attention to when using it:
1) parentsUntil returns an array instead of a single object
Regarding this, just note that 'parents' is a plural number. Indicates that it will return a series of ancestor elements, and the outer elements in the array have larger subscripts.
2) The array returned by parentsUntil does not include objects that meet the selector parameter conditions
This is a bit strange and easily misleading.
3) None of the arrays returned by parentsUntil are objects encapsulated by Jquery. They need to be encapsulated again to use the API## provided by Jquery.
#This is even stranger. Even the official documentation does not explain this. Especially when another API call parent() returns a Jquery object, the return type of parentsUntil() is easily misleading. For example, if you want to get the nearest p container containing a certain element (id="xxx"),Wrong way of writing 1: ##var pparent = $("#xxx").parentsUntil("p"); //parentsUntil returns an array instead of a single element
Wrong way of writing 2: var parents = $("#xxx").parentsUntil("p");
var pparent = parents[parents.length-1]; //The array returned by parentsUntil does not include objects that meet the selector parameter conditions
Wrong writing method 3: var parents = $("#xxx").parentsUntil("p");
var pparent = parents[parents.length- 1].parent(); //None of the returned arrays are objects encapsulated by Jquery. They need to be encapsulated again to use the parent() call provided by Jquery.
Correct writing: var parents = $("#xxx").parentsUntil();
var pparent = $(parents[parents.length-1]).parent();
The above is the detailed content of Summary of three common mistakes in calling parentsUntil in Jquery. For more information, please follow other related articles on the PHP Chinese website!