简体   繁体   English

使用 jquery 和 js 将元素动态添加到元素中

[英]Add <td> elements into <tr> element dynamically using jquery and js

I have table with few tr elements and i want to add a new td for the first tr element dynamically in my js.我有几个 tr 元素的表,我想在我的 js 中动态地为第一个 tr 元素添加一个新的 td。

Table before adding td element.添加 td 元素之前的表格。

<table>
    <tbody>
        <tr>
            <td>A</td>
        <tr>
        <tr>
            <td>D</td>
        <tr>
    <tbody>
<table>

I want a table like this我想要一张这样的桌子

<table>
    <tbody>
        <tr>
            <td>A</td>
            <td>B</td> //Newly addded element
        <tr>
        <tr>
            <td>D</td>
        <tr>
    <tbody>
<table>

I tried looping through tr elements and adding a new td element using jquery in my js file.我尝试循环遍历 tr 元素并在我的 js 文件中使用 jquery 添加一个新的 td 元素。 Like this:像这样:

$('table').each((index,tr)=>{
    if(index === 0){
        tr.append("<td>B</td>");
    }
});

But no lock.但是没有锁。 [Object object] is being rendered in DOM in the place where I added new td. [Object object] 在我添加新 td 的地方以 DOM 呈现。 Something like this:像这样的东西: 在此处输入图像描述

Please suggest me solution to add new td in tr's dynamically.请建议我在 tr 中动态添加新 td 的解决方案。 Thanks in advance.提前致谢。

You don't need a loop for this.你不需要一个循环。 There are lots of selectors/methods you can use to target the first td of the first tr .您可以使用许多选择器/方法来定位第一个tr的第一个td

Any of these will work given the HTML structure in your question:鉴于您的问题中的 HTML 结构,这些中的任何一个都可以工作:

$('table td:first').after('<td>B</td>');

$('<td>B</td>').insertAfter('table td:first');

$('table tr:first').append('<td>B</td>');

$('<td>B</td>').appendTo('table tr:first');

I'm certain there's other ways, but this gives you an idea of the approach to take.我敢肯定还有其他方法,但这让您对要采取的方法有所了解。

Consider the following example.考虑以下示例。

 $(function() { function addCell(content, target) { if(content == undefined){ content = ""; } return $("<td>").html(content).appendTo(target); } addCell("B", $("table tbody tr:eq(0)")); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tbody> <tr> <td>A</td> </tr> <tr> <td>D</td> </tr> </tbody> </table>

I suspect you will have a button or other opportunities to add content.我怀疑您将有一个按钮或其他机会来添加内容。 Creating a small function allows for this to be a bit easier.创建一个小的 function 可以让这更容易一些。 This function accepts the Content, what you want in the Cell, and the Target.这个 function 接受内容、单元格中的内容和目标。

The last part of it will be to provide a Target.最后一部分是提供一个目标。 This can be an HTML Element or a jQuery Object.这可以是 HTML 元素或 jQuery Object。 I provided a jQuery Object with the Selector for the TABLE > TBODY > TR (that is equal to Index 0).我提供了一个 jQuery Object 和 TABLE > TBODY > TR 的选择器(等于索引 0)。

You can also use it in a loop:您也可以在循环中使用它:

$("tbody tr").each(function(i, el){
  if($("td:eq(0)", el).text() == "A"){
    var b = addCell("B", el);
    b.addClass("dynamic");
  }
});

I try this in pure js我用纯js试试这个
This is my code这是我的代码

HTML:- HTML:-

<table>
  <tbody>
      <tr id="myTr">
          <td>A</td>
      <tr>
      <tr>
          <td>D</td>
      <tr>
  <tbody>
<table>

Javascript:- Javascript:-

let myTr = document.getElementById("myTr");
let data = ['B','C'];

data.forEach(item => {
  let td = document.createElement("TD");
  td.innerHTML = item;
  myTr.appendChild(td);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM