Home > Web Front-end > JS Tutorial > body text

jQuery selector: detailed explanation of use cases of only-child

黄舟
Release: 2017-06-23 15:11:46
Original
1160 people have browsed it

Overview

If an element is the only child element in the parent element, it will be matched

If the parent element contains other elements, it will not be matched.

Example

Description:

Find the li that is the only child element in ul

HTML code:

<ul> 
<li>John</li> 
<li>Karl</li> 
<li>Brandon</li> 
</ul> 
<ul> 
<li>Glen</li> 
</ul>
Copy after login

jQuery code:

$("ul li:only-child")
Copy after login

Result:

[ <li>Glen</li> ]
Copy after login

This selector will match the only child element of the parent element. If its parent element contains other elements, it will not be matched.

Grammar structure:

$(":only-child")
Copy after login

This selector is generally used in conjunction with other selectors, such as Class selector, Element selector, etc. wait. For example:

$("li:only-child").css("color","blue")
Copy after login

The above code can set the font color in the li element under the ul element with only one li element to blue.

Note: It is necessary to explain the concept in conjunction with the above code. The parent element mentioned here is not li, but the parent element of li. Many people often mistakenly think that it matches the last element among the child elements of the li element.

Example code:






脚本之家




  • ASP教程
  • html教程
  • DIV+CSS教程
  • jQUERY教程
  • jAVAScript教程
Copy after login

The above is the detailed content of jQuery selector: detailed explanation of use cases of only-child. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template