Home > Web Front-end > JS Tutorial > body text

Quickly learn how to add a row to a table using jQuery

PHPz
Release: 2024-02-28 22:09:03
Original
891 people have browsed it

Quickly learn how to add a row to a table using jQuery

jQuery is a popular JavaScript library that is widely used to simplify web development. Using jQuery in web pages can make the code more concise, easier to maintain, and can achieve many complex functions. This article will teach you how to use jQuery to quickly add a row to a table, allowing you to easily handle adding table data.

First of all, we assume that you already understand the basic syntax of jQuery and how to introduce the jQuery library into a web page. If you're not familiar with jQuery yet, learn some basics before experimenting with this example.

Next, we use a specific case to demonstrate how to use jQuery to add a row to the table. Suppose we have a simple table that contains two columns of data: name and age, as shown below:

<table id="myTable">
  <tr>
    <th>姓名</th>
    <th>年龄</th>
  </tr>
  <tr>
    <td>张三</td>
    <td>25</td>
  </tr>
  <tr>
    <td>李四</td>
    <td>30</td>
  </tr>
</table>
Copy after login

Now we want to implement a function that adds a row of data to the end of the table after clicking a button. First, add a button to the page. When the button is clicked, the operation of adding rows is triggered. The code is as follows:

<button id="addRowBtn">添加行</button>
Copy after login

Next, we use jQuery to implement the function of adding rows. First introduce the jQuery library into the page:

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
Copy after login

Then, add jQuery code at the bottom of the page to implement the function of adding rows:

$(document).ready(function() {
  // 点击按钮触发添加行操作
  $('#addRowBtn').click(function() {
    // 在表格末尾添加一行
    $('#myTable').append('<tr><td>王五</td><td>35</td></tr>');
  });
});
Copy after login

In the above code, we use $('# addRowBtn') to select the button element, and then use the .click() method to add a click event to the button. When the button is clicked, execute the callback function, use $('#myTable') in the function to select the table, and then use the .append() method to add a new row ## at the end of the table #

element and fill it with data. Now you can open the page in the browser, click the button, and you will see a new row of data added to the table. In this way, using jQuery to add a row to the table is achieved.

In general, using jQuery to add a row to a table only requires a few lines of simple code. jQuery's selectors and operation methods make processing DOM elements very convenient and efficient, allowing us to easily operate various elements in web pages. I hope this example is helpful to you, and I hope you can continue to learn jQuery and other front-end technologies and continuously improve your skills.

The above is the detailed content of Quickly learn how to add a row to a table using 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