The empty() function is used to clear all content within each matching element. The
empty() function will remove all child nodes of each matching element (including text nodes, Comment nodes, etc. all types of nodes).
This function belongs to the jQueryobject(instance).
Syntax
jQueryObject.empty( )
Return value
empty()Return value of function is the jQuery type, returning the current jQuery object itself (to facilitate chain-style programming).
Example & Description
empty() function is used to clear all content within each matching element:
<div><p>段落文本1<span></span></p></div> <div><p>段落文本2<span></span></p></div> <!--以上是jQuery代码执行前的html内容--> <script type="text/javascript"> $("p").empty( ); </script> <!--以下是jQuery代码执行后的html内容--> <div><p></p></div> <div><p></p></div>
empty() function and html() function have the following features, etc. Price code:
$("selector").empty( ); // 等价于 $("selector").html("");
Take the following HTML code as an example:
<p id="n1"> <span id="n2">span#n2</span> </p> <p id="n3"> <label id="n4">label#n4</label> <span id="n5">span#n5</span> </p>
The following jQuery sample code is used to demonstrate the specific usage of the empty() function:
// 移除所有p元素中的所有子节点 $("p").empty( );
The above code The complete html code after execution is as follows (the format has not been adjusted in any way):
<p id="n1"></p> <p id="n3"></p>
The above is the detailed content of Detailed explanation of jQuery.empty() function usage. For more information, please follow other related articles on the PHP Chinese website!