Home  >  Article  >  Web Front-end  >  jquery add what does it mean

jquery add what does it mean

青灯夜游
青灯夜游Original
2022-04-20 11:12:523057browse

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)
  • 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>

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>

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!

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