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

Examples of usage of contents() method in jQuery

巴扎黑
Release: 2017-06-24 11:13:41
Original
1883 people have browsed it

This article mainly introduces the usage of the contents() method in jQuery. It analyzes the function and definition of the contents() method and the usage skills of finding all the first-level child nodes inside the matching element. It needs Friends can refer to

This article explains the usage of the contents() method in jQuery with examples. Share it with everyone for your reference. The specific analysis is as follows:

This method finds all first-level child nodes (including text nodes) inside the matching element.
If the element is an iframe, find the document content. The
contents() method is similar to the children() method, except that the former includes text nodes and HTML elements in the resulting jQuery object .

Grammar structure:

The code is as follows:

$(selector).contents()
Copy after login

Instance code:

The code is as follows:

<!DOCTYPE html>  
<html>  
<head>  
<meta charset=" utf-8">  
<meta name="author" content="http://www.jb51.net/" /> 
<title>contents()函数-脚本之家</title> 
<script type="text/
javascript
" src="mytest/jQuery/jquery-1.8.3.js"></script> 
<script type="text/javascript"> 
$(
document
).ready(function(){ 
  $("ul").contents().not("[nodeType==1]").css("color","red"); 
}) 
</script> 
</head> 
<body> 
<p> 
  <ul> 
    <li>html专区</li> 
    <li>p+CSS专区</li> 
    <li>Javascript专区</li> 
    <li>Jquery专区</li> 
  </ul> 
</p> 
</body> 
</html>
Copy after login

The above code can set the color of the text node under the ul element to red.

The above is the detailed content of Examples of usage of contents() method in jQuery. 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