jQuery .empty() 详细讲解

news/2024/7/4 7:11:46

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

.empty()

摘要

    Categories: Manipulation > DOM Removal

    Version added: 1.0.empty()

    DescriptionRemove all child nodes of the set of matched elements from the DOM.

    Extra: This method does not accept any arguments

 

用法

    This method removes not only child (and other descendant) elements, but also any text within the set of matched elements. This is because, according to the DOM specification, any string of text within an element is considered a child node of that element. Consider the following HTML:

<div class="container">
  <div class="hello">Hello</div>
  <div class="goodbye">Goodbye</div>
</div>

    We can target any element for removal:

	
$( ".hello" ).empty();

    This will result in a DOM structure with the Hello text deleted:

<div class="container">
  <div class="hello"></div>
  <div class="goodbye">Goodbye</div>
</div>

    If we had any number of nested elements inside <div class="hello">, they would be removed, too.

    To avoid memory leaks, jQuery removes other constructs such as data and event handlers from the child elements before removing the elements themselves.

    If you want to remove elements without destroying their data or event handlers (so they can be re-added later), use .detach()instead.

示例

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>empty demo</title>
  <style>
  p {
    background: yellow;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
 
<p>
  Hello, <span>Person</span> <em>and person</em>.
</p>
 
<button>Call empty() on above paragraph</button>
 
<script>
$( "button" ).click(function() {
  $( "p" ).empty();
});
</script>
 
</body>
</html>

104854_DkpQ_1864705.png

转载于:https://my.oschina.net/leblancs/blog/790677


http://www.niftyadmin.cn/n/4458417.html

相关文章

中国人奇强:十大最成功国产软件排行(图)

[精华]中国人奇强&#xff1a;十大最成功国产软件排行(图) shiyjru发表于 2005-12-6 22:12 1楼 注册时间&#xff1a;2005-3-19<!--[个人文集]--> [加为好友] [发短消息] shiyjru 当前离线 <!-- --><!----> 头 衔&#xff1a;论坛新兵 威 望&#xff1a…

mybatis支持属性使用驼峰的命名

在基本的 MyBatis 中,session 工厂可以使用 SqlSessionFactoryBuilder 来创建。而在 MyBatis-spring 中,则使用 SqlSessionFactoryBean 来替代。 typeAliasesPackage属性 该属性可以给包中的类注册别名&#xff0c;注册后可以直接使用类名(在mybatis的XXmapper.xml配置文件中直…

mysql中FIND_IN_SET函数的使用

有种需求&#xff0c;A和B是父子关系&#xff0c;B和C是父子关系&#xff0c;C与D亦是父子关系&#xff0c;以此类推&#xff0c;无限级 现在需要查询到某一级&#xff08;包括本级&#xff09;下面所有的&#xff0c;就需要用到FIND_IN_SET函数 select * from table where FIN…

2011年世界著名的杀毒软件排名是怎样的?

2011年世界著名的杀毒软件排名是怎样的&#xff1f; 2011-3-26 16:45 提问者&#xff1a;柳楦梓 | 浏览次数&#xff1a;8656次2011-3-26 20:02 最佳答案 国外有5、6个病毒实验室都有杀软排名&#xff0c;都是专业的&#xff0c;你要看哪个&#xff1f;我给你介绍。告诉你&…

【zTree】 zTree使用的 小例子

使用zTree树不是第一次了 但是 还是翻阅着之前做的 对照着 使用起来比较方便 这里就把小例子列出来 总结一下使用步骤 这样方便下次使用起来方便一点 使用zTree树的步骤&#xff1a; 1.首先 在jsp中引用zTree相关的 CSS文件和JS文件 2.其次 在jsp中设置zTree的容器&#…

一个本土科技精英的近况

一个本土科技精英的近况 【设置字体:大 中 小】【关闭】 原谅我的孤陋寡闻&#xff0c;我今天才知道梁肇新先生是我们广西人。我为广西大学能出这样的精英而感到自豪。想当年豪杰超级解霸可是我们电脑上必装的软件之一&#xff0c;可惜后来风光不再了。但是今天又看到有关他和他…

金山IPO造富效应:中国最贵程序员群生态

金山IPO造富效应&#xff1a;中国最贵程序员群生态 日期&#xff1a;2007-10-11 作者&#xff1a;侯继勇 出处&#xff1a;我来说两句()10月9日&#xff0c;金山软件(HK.3888)以首日大涨39%的成绩在香港联交所正式挂牌。 下午还没收市&#xff0c;金山软件董事长求伯君和CEO雷…

Kali Linux中DNS分析工具总结

dnsenum的目的是尽可能收集一个域的信息&#xff0c;它能够通过谷歌或者字典文件猜测可能存在的域名&#xff0c;以及对一个网段进行反向查询。它可以查询网站的主机地址信息、域名服务器、mx record&#xff08;函件交换记录&#xff09;&#xff0c;在域名服务器上执行axfr请…