Home > Web Front-end > Front-end Q&A > jquery add what does it mean

jquery add what does it mean

青灯夜游
Release: 2022-04-20 18:39:55
Original
3092 people have browsed it

add means "add" and is a built-in method for adding elements to an existing element combination. The syntax is "$(element).add(element,context)"; this method accepts two parameters. , respectively specify the element to be added and the adding position. The second parameter is optional. If set, the element will only be added to the context element.

jquery add what does it mean

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

In jquery, add means "add".

jquery add what does it mean

#add() method adds elements to an existing combination of elements.

This method accepts two parameters, specifying the element to be added and the location to add:

$(selector).add(element,context)
Copy after login
  • element: required. Specifies a selector expression, jQuery object, one or more elements, or HTML fragment to be added to an existing collection of elements.

  • #context: Optional. Specifies the position in the document where the selector expression begins matching. The

#add() method will add the element to the entire document. If the context parameter is specified, it will only be added to the context element.

Example 1:Use an HTML fragment to add a element to an existing

element.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<script src="js/jquery-1.10.2.min.js"></script>
		<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
		</script>
		<script>
			$(document).ready(function() {
				$("button").click(function() {
					$("p").add("<br><span>一个新的span元素。</span>").appendTo("p");
				});
			});
		</script>

	</head>
	<body>
		<button>添加一个span元素</button>
		<p>一个P元素。</p>

	</body>
</html>
Copy after login

jquery add what does it mean

Example 2: Add

and elements to an existing element combination (

) :

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<script src="js/jquery-1.10.2.min.js"></script>
		<script>
			$(document).ready(function() {
				$("h1").add("p").add("span").css("background-color", "yellow");
			});
		</script>
	</head>
	<body>

		<h1>欢迎</h1>
		<p>一个P元素。</p>
		<p>另一个P元素。</p>
		<span>一个span元素。</span>
		<span>一个span元素。</span><br><br>
		<div>本示例为现有的H1元素添加相同的CSS样式为p和span元素。</div>

	</body>
</html>
Copy after login

jquery add what does it mean

[Recommended learning: jQuery video tutorial, web front-end video]

The above is the detailed content of jquery add what does it mean. 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