Home  >  Article  >  Web Front-end  >  How to delete child elements in td with jquery

How to delete child elements in td with jquery

WBOY
WBOYOriginal
2022-05-12 11:01:221731browse

Method: 1. Use the children() method to obtain the child element object of the td element. This method can return the direct child element of the selected element. The syntax is "td element object.children()"; 2. Use The remove() method can delete the obtained sub-element, and the syntax is "td sub-element object.remove()".

How to delete child elements in td with jquery

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

How to delete child elements in td with jquery

1. Use the children() method to obtain child elements

The children() method returns the selected element All direct child elements of .

Syntax

$(selector).children(filter)

filter Optional. Specifies a selector expression that narrows the search for child elements.

2. Use the remove() method to delete elements

The remove() method removes the selected elements, including all text and child nodes.

This method will also remove the data and events of the selected element.

Grammar

$(selector).remove()

The example is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>123</title>
<script src="js/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("td").children().remove();
});
});
</script>
</head>
<body >
<table border="1">
  <tr>
    <td>Cell<i>A</i></td>
    <td>Cell<i>B</i></td>
  </tr>
</table>
<button>移除td子元素</button>
</body>
</html>

Output result:

How to delete child elements in td with jquery

Related video tutorial recommendations: jQuery video tutorial

The above is the detailed content of How to delete child elements in td with jquery. 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
Previous article:Is layui based on jquery?Next article:Is layui based on jquery?