Home > Article > Web Front-end > jquery add what does it mean
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.
The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.
In jquery, add means "add".
#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. Example 2: Add and elements to an existing element combination ( [Recommended learning: jQuery video tutorial, web front-end video]<!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>
) :
<!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>
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!