Home  >  Article  >  Web Front-end  >  Summary of three common mistakes in calling parentsUntil in Jquery

Summary of three common mistakes in calling parentsUntil in Jquery

巴扎黑
巴扎黑Original
2017-06-24 09:53:361012browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn