This article mainly introduces the usage of prevUntil() method in jQuery. The example analyzes the usage skills of finding all peer elements before matching elements according to conditions. Friends in need can refer to it
The example in this article describes the usage of the prevUntil() method in jQuery. Share it with everyone for your reference. The specific analysis is as follows:
This function searches for all sibling elements before the matching element until it encounters an exprexpression, DOM element or jQuery element that matches up to that element.
You can use optional expressions to filter the collection of sibling elements.
Note: Ancestor elements do not contain elements matching expr expressions, DOM elements or jQuery elements.
Grammar:
Grammar 1:
The code is as follows:
$(selector).prevUntil(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> <style type="text/css"> .father { height:200px; width:200px; border:1px solid blue; } </style> <script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(".father p").prevUntil(document.getElementById("firstp")).css("color","blue") }) </script> </head> <body> <p class="father"> <p id="firstp">我是p元素</p> <span>我是span元素</span> <p>我是p元素</p> <p>我是p元素</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> <style type="text/css"> .father { height:200px; width:200px; border:1px solid blue; } </style> <script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(".father p").prevUntil(document.getElementById("firstp"),"span").css("color","blue") }) </script> </head> <body> <p class="father"> <p id="firstp">我是p元素</p> <span>我是span元素</span> <p>我是p元素</p> <p>我是p元素</p> </p> </body> </html>
The above is the detailed content of Analysis of examples using the prevUntil() method in jQuery. For more information, please follow other related articles on the PHP Chinese website!