This article mainly introduces the usage of the parentsUntil() method in jQuery. The example analyzes the function and definition of the parentsUntil() method and searches for all ancestors of matching elements based on conditions. For tips on using elements, friends in need can refer to
This article explains the usage of the parentsUntil() method in jQuery with examples. Share it with everyone for your reference. The specific analysis is as follows:
This method finds all ancestor elements of the matching element until it encounters the element that matches exprexpression, DOM element or jQuery element.
Obtaining the collection of ancestor elements can be filtered by filtering expressions.
Note: Ancestor elements do not contain elements matching expr expressions, DOM elements or jQuery elements.
Grammar:
Grammar 1:
The code is as follows:
$(selector).parentsUntil(expr,filter)
Parameter list:
实例:
实例一:
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="author" content="http://www.jb51.net/" /> <title>脚本之家</title> <script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("li").parentsUntil(document.getElementById("inner")).css("border","1px solid blue"); }) </script> </head> <body> <p class="outer"> <p>脚本之家欢迎您</p> <p id="inner"> <ul class="first"> <li>HTML专区</li> <li>Javascript专区</li> <li>p+CSS专区</li> <li>Jquery专区</li> </ul> <ul class="second"> <li>HTML专区</li> <li>Javascript专区</li> <li>p+CSS专区</li> <li>Jquery专区</li> </ul> </p> </p> </body> </html>
实例二:
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="author" content="http://www.jb51.net/" /> <title>脚本之家</title> <script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("li").parentsUntil(document.getElementById("inner"),".second").css("border","1px solid blue"); }) </script> </head> <body> <p class="outer"> <p>脚本之家欢迎您</p> <p id="inner"> <ul class="first"> <li>HTML专区</li> <li>Javascript专区</li> <li>p+CSS专区</li> <li>Jquery专区</li> </ul> <ul class="second"> <li>HTML专区</li> <li>Javascript专区</li> <li>p+CSS专区</li> <li>Jquery专区</li> </ul> </p> </p> </body> </html>
The above is the detailed content of Tutorial on using parentsUntil() method in jQuery. For more information, please follow other related articles on the PHP Chinese website!