Home  >  Article  >  Web Front-end  >  What are the jquery search methods?

What are the jquery search methods?

青灯夜游
青灯夜游Original
2023-01-28 13:56:252185browse

The search methods are: 1. parent(), which can find the parent element of the current element; 2. parents(), which can find the ancestor elements of the current element; 3. children(), which can find the children of the current element. element; 4. find(), which can find descendant elements of the current element; 5. contents(); 6. siblings(); 7. next(); 8. nextAll(); 9. nextUntil(); 10. prev( ); 11. prevUntil() and so on.

What are the jquery search methods?

The operating environment of this tutorial: windows7 system, jquery3.6 version, Dell G3 computer.

In order to operate elements more flexibly, in addition to selectors, jQuery also provides us with two ways in the form of "methods": one is "filter method"; the other is "find method". The filtering method and the search method are actually a supplement to the jQuery selector.

Filtering method refers to further filtering the selected elements. The search method mainly uses the currently selected element as the base point to find the parent element, child element or sibling element of this element.

In jQuery, for the search method, we can divide it into the following three situations.

  • Find ancestor elements

  • Find descendant elements

  • Find sibling elements

jQuery Finds Ancestor Elements

In jQuery, if you want to find the ancestor elements of the current element (parent element, parent element, etc.) , we have the following 3 methods.

  • (1)parent()

  • (2)parents()

  • (3 ) parentsUntil()

1, parent()

In jQuery, we can use the parent() method to find the " parent element". Note that elements have only one parent element.

Syntax:

$.parent(selector)

Description:

selector is an optional parameter, which is a selector used to find parent elements that meet the conditions. When the parameter is omitted, it means that the parent element does not need to meet any conditions; when the parameter is not omitted, it means that the parent element does not need to meet the condition.

Example: parent() without parameters

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <style type="text/css">
        table, tr, td{border:1px solid silver;}
        td
        {
            width:40px;
            height:40px;
            line-height:40px;
            text-align:center;
        }
    </style>
    <script src="js/jquery-1.12.4.min.js"></script>
    <script>
        $(function(){
            $("td").hover(function () {
                $(this).parent().css("background-color", "silver");
            }, function () {
                $(this).parent().css("background-color", "white");
            })
        })
    </script>
</head>
<body>
    <table>
        <tr>
            <td>2</td>
            <td>4</td>
            <td>8</td>
        </tr>
        <tr>
            <td>16</td>
            <td>32</td>
            <td>64</td>
        </tr>
        <tr>
            <td>128</td>
            <td>256</td>
            <td>512</td>
        </tr>
    </table>
</body>
</html>

By default, the preview effect is as shown below.

What are the jquery search methods?

When the mouse moves over a certain cell, the preview effect is as shown below.

What are the jquery search methods?

Analysis:

$(this).parent() means selecting the parent element (tr) of the current td element, but the parent element (table) does not will be selected.

Example: parent() with parameters

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <style type="text/css">
        table, tr, td{border:1px solid silver;}
        td
        {
            width:40px;
            height:40px;
            line-height:40px;
            text-align:center;
        }
    </style>
    <script src="js/jquery-1.12.4.min.js"></script>
    <script>
        $(function(){
            $("td").hover(function () {
                $(this).parent(".select").css("background-color", "silver")
            }, function () {
                $(this).parent(".select").css("background-color", "white")
            })
        })
    </script>
</head>
<body>
    <table>
        <tr>
            <td>2</td>
            <td>4</td>
            <td>8</td>
        </tr>
        <tr class="select">
            <td>16</td>
            <td>32</td>
            <td>64</td>
        </tr>
        <tr>
            <td>128</td>
            <td>256</td>
            <td>512</td>
        </tr>
    </table>
</body>
</html>

By default, the preview effect is as shown below.

What are the jquery search methods?

When the mouse moves over the td element of class="select", the preview effect is as shown in the figure below.

What are the jquery search methods?

Analysis:

$(this).parent(".select") means selecting the parent element of the current td element ( tr), and the parent element must contain the class name "select".

2. parents()

In jQuery, we can use the parents() method to find the "ancestor elements" of the current element. Note that elements can have multiple ancestor elements

The two methods parent() and parents() are easy to distinguish. Among them, parent() is a singular number, so there is only one element to be searched for, which is the parent element. parents() is a plural number, so there are multiple elements to be searched, that is, all ancestor elements (including parent elements, grandfather elements, etc.).

Syntax:

$().parents(selector)

Description:

selector is an optional parameter, which is a selector used to find ancestor elements that meet the conditions. When the parameter is omitted, it means that the ancestor element does not need to meet any conditions; when the parameter is not omitted, it means that the ancestor element needs to meet the condition.

Example: Find all ancestor elements

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="js/jquery-1.12.4.min.js"></script>
    <script>
        $(function(){
            var parents = $("span").parents();
            var result = [];
            $.each(parents, function(){
                var item = this.tagName.toLowerCase();
                result.push(item);
            });
            console.log(result.join(","));
        })
    </script>
</head>
<body>
    <div>
        <p>
            <strong>
                <span>绿叶学习网</span>
            </strong>
        </p>
    </div>
</body>
</html>

The console output is as shown below.

What are the jquery search methods?

Analysis:

("span").parents() returns a collection of jQuery objects, in this example , we use .each() to traverse all ancestor elements of the span element. For the .each() method, we will introduce it in detail in subsequent chapters. Some friends will ask: "To get the tag name of an element, shouldn't you use (this).tagName? Why is this.tagName used here?"

$(this) is a jQuery object , which calls jQuery methods or properties, such as click(), keyup(), etc. this is a DOM object, which calls methods or properties of the DOM object, such as this.id, this.value, etc. Since the tagName attribute belongs to the DOM object, we use this.tagName here.

3、parentsUntil()

在jQuery中,parentsUntil()方法是parents()方法的一个补充,它可以查找“指定范围”的所有祖先元素,相当于在parents()方法返回的集合中截取一部分。

语法:

$().parentsUntil(selecotr)

说明:

selector是一个可选参数,它是一个选择器,用来选择符合条件的祖先元素。

举例:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="js/jquery-1.12.4.min.js"></script>
    <script>
        $(function(){
            var parents = $("span").parentsUntil("div");
            var result = [];
            $.each(parents, function(){
                var item = this.tagName.toLowerCase();
                result.push(item);
            });
            console.log(result.join(","));
        })
    </script>
</head>
<body>
    <div>
        <p>
            <strong>
                <span>绿叶学习网</span>
            </strong>
        </p>
    </div>
</body>
</html>

控制台输出结果如下图所示。

What are the jquery search methods?

分析:

在实际开发中,我们一般只会用到parent()方法和parents()这两个,极少用到parentsUntil()。因此对于parentsUntil()方法,我们了解一下就行。

jQuery查找后代元素

在jQuery中,如果想要查找当前元素的后代元素(子元素、孙元素等),我们有以下3种方法:

  • (1)children()
  • (2)find()
  • (3)contents()

1、children()

在jQuery中,我们可以使用children()方法来查找当前元素的“子元素”。注意,children()方法只能查找子元素,不能查找其他后代元素。

语法:

$().children(selector)

说明:

selector是一个可选参数,它是一个选择器,用来查找符合条件的子元素。当参数省略,表示选择所有子元素;当参数不省略时,表示选择符合条件的子元素。

举例:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <style lang="">
        p{margin:6px 0;}
    </style>
    <script src="js/jquery-1.12.4.min.js"></script>
    <script>
        $(function(){
            $("#wrapper").hover(function(){
                $(this).children(".select").css("color", "red");
            },function(){
                $(this).children(".select").css("color", "black");
            })
        })
    </script>
</head>
<body>
    <div id="wrapper">
        <p>子元素</p>
        <p>子元素</p>
        <div>
            <p>孙元素</p>
            <p>孙元素</p>
        </div>
        <p>子元素</p>
        <p>子元素</p>
    </div>
</body>
</html>

默认情况下,预览效果如下图所示。

What are the jquery search methods?

当鼠标移到id="wrapper"的div元素上面,此时预览效果如下图所示。

What are the jquery search methods?

分析:

$(this).children(".select")表示获取当前元素下的class="select"的子元素。我们可以发现,class="select"的孙元素却不会被选中。

2、find()

在jQuery中,我们可以使用find()方法来查找当前元素的“后代元素”。注意,find()方法不仅能查找子元素,还能查找其他后代元素。

语法:

$().find(selector)

说明:

selector是一个可选参数,它是一个选择器,用来查找符合条件的后代元素。当参数省略,表示选择所有后代元素;当参数不省略时,表示选择符合条件的后代元素。

举例:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <style lang="">
        p{margin:6px 0;}
    </style>
    <script src="js/jquery-1.12.4.min.js"></script>
    <script>
        $(function(){
            $("#wrapper").hover(function(){
                $(this).find(".select").css("color", "red");
            },function(){
                $(this).find(".select").css("color", "black");
            })
        })
    </script>
</head>
<body>
    <div id="wrapper">
        <p>子元素</p>
        <p>子元素</p>
        <div>
            <p>孙元素</p>
            <p>孙元素</p>
        </div>
        <p>子元素</p>
        <p>子元素</p>
    </div>
</body>
</html>

默认情况下,预览效果如下图所示。

What are the jquery search methods?

当鼠标移到id="wrapper"的div元素上面,此时预览效果如下图所示。

What are the jquery search methods?

分析:

$(this).find(".select")表示不仅会选取当前元素下的class="select"的子元素,还会选取class="select"的孙元素。

3、contents()

在jQuery中,我们可以使用contents()方法来获取子元素及其内部文本。contents()方法和children()方法相似,不同的是,contents()返回的jQuery对象中不仅包含子元素,还包含文本内容。而children()方法返回的jQuery对象中只会包含子元素,不包含文本内容。

在实际开发中,我们极少会用到contents()方法,因此小伙伴们不需要深入了解,这里简单认识一下即可。

jQuery查找兄弟元素

兄弟元素,指的是该元素在同一个父元素下的同级元素。

在jquery中,查询同级元素一般有七个方法:siblings()、next()、nextAll()、nextUntil()、prev()、prevAll()、prevUntil()

  • siblings()方法,主要用于获得指定元素的同级所有元素

  • next()方法,主要用于获得指定元素的下一个同级元素

  • nextAll()方法,主要用于获得指定元素的下一个同级的所有元素

  • nextUntil()方法,主要用于获得指定元素的下一个同级元素,这个同级元素必须为指定元素与nextUntil()方法所设置元素之间的元素

  • prev()方法,主要用于获得指定元素的上一级同级元素

  • prevAll()方法,主要用于获得指定元素上一级所有的同级元素

  • prevUntil()方法,主要用于获得指定元素的上一个同级元素,这个同级元素必须为指定元素与prevUntil()方法所设置元素之间的元素

siblings()方法

<!DOCTYPE html>
<html>
	<head>
		<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
	</head>

	<body>
		<div><span>Hello</span></div>
		<p class="selected">Hello Again</p>
		<p>And Again</p>

		<script>
			$("p").siblings(".selected").css("background", "yellow");
		</script>

	</body>
</html>

What are the jquery search methods?

next()方法

<!DOCTYPE html>
<html>
	<head>
		<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
	</head>

	<body>
		<ul>
			<li>list item 1</li>
			<li>list item 2</li>
			<li class="third-item">list item 3</li>
			<li>list item 4</li>
			<li>list item 5</li>
		</ul>

		<script>
			$(&#39;li.third-item&#39;).next().css(&#39;background-color&#39;, &#39;red&#39;);
		</script>

	</body>
</html>

What are the jquery search methods?

nextAll()方法

<!DOCTYPE html>
<html>
	<head>
		<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
	</head>

	<body>

		<ul>
			<li>list item 1</li>
			<li>list item 2</li>
			<li class="third-item">list item 3</li>
			<li>list item 4</li>
			<li>list item 5</li>
		</ul>

		<script>
			$(&#39;li.third-item&#39;).nextAll().css(&#39;background-color&#39;, &#39;red&#39;);
		</script>

	</body>
</html>

What are the jquery search methods?

nextUntil()方法

<!DOCTYPE html>
<html>
	<head>
		<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
		<style>
			.siblings * {
				display: block;
				border: 2px solid lightgrey;
				color: lightgrey;
				padding: 5px;
				margin: 15px;
			}
		</style>
		<script>
			$(document).ready(function() {
				$("li.start").nextUntil("li.stop").css({
					"color": "red",
					"border": "2px solid red"
				});
			});
		</script>
	</head>
	<body>

		<div style="width:500px;" class="siblings">
			<ul>ul (父节点)
				<li>li (兄弟节点)</li>
				<li>li (兄弟节点)</li>
				<li class="start">li (类名为"start"的兄弟节点)</li>
				<li>li (类名为"start"的li节点的下一个兄弟节点)</li>
				<li>li (类名为"start"的li节点的下一个兄弟节点)</li>
				<li>li (类名为"start"的li节点的下一个兄弟节点)</li>
				<li class="stop">li (类名为"stop"的兄弟节点)</li>
			</ul>
		</div>
		<p>在这个例子中,我们返回在类名为“star”和类名为“stop”的 li元素之间的所有下一个兄弟元素。</p>

	</body>
</html>

What are the jquery search methods?

prev()方法

<!DOCTYPE html>
<html>
	<head>
		<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
		<style>
			.siblings * {
				display: block;
				border: 2px solid lightgrey;
				color: lightgrey;
				padding: 5px;
				margin: 15px;
			}
		</style>
		<script>
			$(document).ready(function() {
				$("li.start").prev().css({
					"color": "red",
					"border": "2px solid red"
				});
			});
		</script>
	</head>
	<body>

		<div style="width:500px;" class="siblings">
			<ul>ul (父节点)
				<li>li (兄弟节点)</li>
				<li>li (类名为"start"的li节点的上一个兄弟节点)</li>
				<li class="start">li (类名为"start"的li节点)</li>
				<li>li (兄弟节点)</li>
				<li>li (兄弟节点)</li>
			</ul>
		</div>

	</body>
</html>

What are the jquery search methods?

prevAll()方法

<!DOCTYPE html>
<html>
	<head>
		<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
		<style>
			.siblings * {
				display: block;
				border: 2px solid lightgrey;
				color: lightgrey;
				padding: 5px;
				margin: 15px;
			}
		</style>
		<script>
			$(document).ready(function() {
				$("li.start").prevAll().css({
					"color": "red",
					"border": "2px solid red"
				});
			});
		</script>
	</head>
	<body>

		<div style="width:500px;" class="siblings">
			<ul>ul (parent)
				<li>li (类名为"start"的li的上一个兄弟节点)</li>
				<li>li (类名为"start"的li的上一个兄弟节点)</li>
				<li>li (类名为"start"的li的上一个兄弟节点)</li>
				<li class="start">li (类名为"start"的li节点)</li>
				<li>li (兄弟节点)</li>
				<li>li (兄弟节点)</li>
			</ul>
		</div>
		<p>在这个例子中,我们返回类名称为“star”的li元素之前的所有兄弟元素。</p>

	</body>
</html>

What are the jquery search methods?

prevUntil()方法

<!DOCTYPE html>
<html>
	<head>
		<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
		<style>
			.siblings * {
				display: block;
				border: 2px solid lightgrey;
				color: lightgrey;
				padding: 5px;
				margin: 15px;
			}
		</style>
		<script>
			$(document).ready(function() {
				$("li.start").prevUntil("li.stop").css({
					"color": "red",
					"border": "2px solid red"
				});
			});
		</script>
	</head>
	<body>

		<div style="width:500px;" class="siblings">
			<ul>ul (父节点)
				<li class="stop">li (类名为"stop"的兄弟节点)</li>
				<li>li (类名为"start"的li节点的上一个兄弟节点)</li>
				<li>li (类名为"start"的li节点的上一个兄弟节点)</li>
				<li>li (类名为"start"的li节点的上一个兄弟节点)</li>
				<li class="start">li (类名为"start"的li节点)</li>
				<li>li (兄弟节点)</li>
				<li>li (兄弟节点)</li>
			</ul>
		</div>
		<p>在这个例子中,我们返回在类名为“star”和“stop”的li元素之间的所有上一个兄弟元素,。</p>

	</body>
</html>

What are the jquery search methods?

【推荐学习:jQuery视频教程web前端视频

The above is the detailed content of What are the jquery search methods?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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