The jquery-based has () method and its differences with the find () method and filter () method

Source: Internet
Author: User

Has (selector or DOM element) matches the Element Set Based on the selector or DOM element as the condition to retrieve whether the condition exists in the descendant of each element, create a new result set with the matching elements.

The following is an example:
Copy codeThe Code is as follows:
<Ul>
<Li> list item 1 </li>
<Li> list item 2
<Ul>
<Li> <div> <span> a </span> </div> list item 2-a </li>
<Li> list item 2-b </li>
</Ul>
</Li>
<Li> list item 3 </li>
<Li> list item 4 </li>
</Ul>

Copy codeThe Code is as follows:
Certificate ('li'background .has('span'background .css ('background-color', 'red ');

The result is as follows:

In this example, we can see that, when matching li, we need to check whether the child element of the li contains span. If it contains, the element is included in the result. If it is not included, it is excluded.

Note: has only plays a judgment role. Use the selector or DOM element in the has parameter as the condition to check whether the elements in the original result set match. Remove the non-conforming elements to form a new result set.

This is different from the find () method. The find () method is used to obtain the descendant of each element in the current result set. Parameters (selector, jquery set, or DOM element) are used as filter conditions. If the filter conditions are met, they are retained and the descendant is retained. In the has () method, a parameter is only used as a condition. If the condition is met, its pre-element is added to a new result set, rather than the new result set to be added to the descendant.

$('li').find('span').css('background-color', 'red');

Result:


Parameters in the filter () and has () methods are filter conditions. The difference is the filter () method, where the condition acts on itself; the has () method condition acts on its descendant element.

Take the following example: in the filter () method, Conditions Act on itself.LiThe has () method must act onDescendant element of li
Copy codeThe Code is as follows:
<Ul>
<Li class = "a"> list item 1 </li>
<Li> list item 2
<Ul>
<Li> <div> <span> a </span> </div> list item 2-a </li>
<Li> list item 2-b </li>
</Ul>
</Li>
<Li> list item 3 </li>
<Li> list item 4 </li>
</Ul>

Copy codeThe Code is as follows:
Certificate ('li'0000.filter('.a'0000.css ('background-color', 'red ');

Result:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.