Home > Article > Web Front-end > Examples of usage of contents() method in jQuery
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()
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>
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!