Home > Web Front-end > Front-end Q&A > How to add a row of tables in jquery

How to add a row of tables in jquery

青灯夜游
Release: 2022-05-30 14:49:38
Original
2859 people have browsed it

Two methods: 1. Use append() to add tr sub-elements, the syntax is "$("table").append("

td data")". 2. Use appendTo(), the syntax is "$("td data").appendTo("table");".

How to add a row of tables in jquery

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

Adding a row of data to the table is to add the tr sub-element to the table element. The following are two methods:

Method 1: append() method

append( ) method inserts content to the "end" inside the selected element.

$(A).append(B)
Copy after login

means inserting B at the end of A.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<script src="js/jquery-1.10.2.min.js"></script>
		<script>
			$(document).ready(function() {
				$("button").on("click", function() {
					$("table").append("<tr><td>数据</td><td>数据</td></tr>");
				});
			});
		</script>
	</head>

	<body class="ancestors">
		<table border="1">
			<tr>
				<th>商品</th>
				<th>价格</th>
			</tr>
			<tr>
				<td>T恤</td>
				<td>¥100</td>
			</tr>
			<tr>
				<td>牛仔褂</td>
				<td>¥250</td>
			</tr>
			<tr>
				<td>牛仔库</td>
				<td>¥150</td>
			</tr>
		</table><br>
		<button>增加一行表格</button>
	</body>

</html>
Copy after login

How to add a row of tables in jquery

Method 2: appendTo( ) method

appendTo( ) and append( ) Although the functions of these two methods are similar, Both insert content "at the end" inside the selected element, but the operating objects of the two are reversed.

$(A).appendTo(B)
Copy after login
  • $(A).appendTo(B) means inserting A into the end of B.

<script>
	$(document).ready(function() {
		$("button").on("click", function() {
			$("<tr><td>数据</td><td>数据</td></tr>").appendTo("table");
		});
	});
</script>
Copy after login

How to add a row of tables in jquery

[Recommended learning: jQuery video tutorial, web front-end video

The above is the detailed content of How to add a row of tables in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template