A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈君 金牌黑马   /  2014-8-6 17:56  /  807 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

转自:http://www.jb51.net/article/53199.htm
删除HTML元素可以使用remove及empty,下面有关这两个方法的使用将以示例的方式为大家介绍下jQuery使用下面两个方法来删除或是清空某个HTML元素。
remove() – 删除指定的元素(包括其子元素)
empty() – 清空指定元素的子元素
例如:
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JQuery Demo</title>
  6. <script src="scripts/jquery-1.9.1.js"></script>
  7. <script>
  8. $(document).ready(function () {
  9. $("button").click(function () {
  10. $("#div1").remove();
  11. });
  12. });
  13. </script>
  14. </head>
  15. <body>

  16. <div id="div1" style="height: 100px; width: 300px;
  17. border: 1px solid black; background-color: yellow;">
  18. This is some text in the div.
  19. <p>This is a paragraph in the div.</p>
  20. <p>This is another paragraph in the div.</p>

  21. </div>
  22. <br>
  23. <button>Remove div element</button>

  24. </body>
  25. </html>
  26. empty:
  27. <!DOCTYPE html>
  28. <html>
  29. <head>
  30. <meta charset="utf-8">
  31. <title>JQuery Demo</title>
  32. <script src="scripts/jquery-1.9.1.js"></script>
  33. <script>
  34. $(document).ready(function () {
  35. $("button").click(function () {
  36. $("#div1").empty();
  37. });
  38. });
  39. </script>
  40. </head>
  41. <body>

  42. <div id="div1" style="height: 100px; width: 300px;
  43. border: 1px solid black; background-color: yellow;">
  44. This is some text in the div.
  45. <p>This is a paragraph in the div.</p>
  46. <p>This is another paragraph in the div.</p>

  47. </div>
  48. <br>
  49. <button>Empty the div element</button>

  50. </body>
  51. </html>
复制代码

jQuery的remove()方法也支持一个参数,可以用于过滤一些需要删除的HTML元素。这个参数可以为任何有效的jQuery selector.
比如下面代码只删除class=”italic”的<p>元素:$("p").remove(".italic");
















































0 个回复

您需要登录后才可以回帖 登录 | 加入黑马