Home  >  Article  >  Web Front-end  >  In jQuery: has selector usage example_jquery

In jQuery: has selector usage example_jquery

WBOY
WBOYOriginal
2016-05-16 16:23:121243browse

The example in this article describes the usage of has selector in jQuery. Share it with everyone for your reference. The specific analysis is as follows:

This selector matches elements that contain the element matched by the selector.

Grammar structure:

Copy code The code is as follows:
$(":has(selector)")

The definition of this selector may be a bit confusing. In layman's terms, if an element contains an element matched by the selector (selector), then this element will be matched. For example:
Copy code The code is as follows:
$("div:has(p)")

The above selector will match div elements containing p elements.
This selector is generally used in conjunction with other selectors, such as class selectors, element selectors, etc. . For example:
Copy code The code is as follows:
$("div:has(p)").css("color", "blue")

The above code sets the font color in the div element containing the p element to blue.
If not used with other selectors, the default state is to be used with the * selector, for example, $(":has") is equivalent to $("*:has").

Parameter list:

参数 描述
selector 一个用于筛选的选择器。

Example code:

Example 1:

Copy code The code is as follows:






Script Home





I don’t include span

I am span





The above code can set the border color of the div containing the span element to red.

Example 2:

Copy code The code is as follows:






Script Home





I don’t include span

I am span

I am span






Since the above code does not specify a selector to be used with the :has selector, it is used with the * selector by default, so the above code can set the border color of all elements containing all span elements to red.

I hope this article will be helpful to everyone’s jQuery programming.

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