简体   繁体   中英

Jquery add <td> to table by <tr id>

Can i do that using jquery? Unfortunately i don't have a table id, only a tr id..

<table class="tbclass" align="center" width="100%">
<tbody>
    <tr id="tr_1#?#1" align="center" class="tbrclass">
        <my td inserted here with jquery>
        </td>
        <td class="tbdclass" style="text-align:left;vertical-align:middle;font-weight:bold;">
            Hello
        </td>
    </tr>
</tbody>

You are trying to modify the DOM by injecting only an opening tag, when you already have the closing tag. You can't do that. First, your existing HTML is invalid. Second, that's not how DOM manipulation works.

Take out the stray closing tag, then use

$('#yourtrid').append('<td>newtd</td>')

EDIT: The code in question is this:

    <my td inserted here with jquery>
    </td>

I am reading this to mean that you already have </td> and want to put <td ...>something where you currently have <my td inserted here with jquery> . If that's incorrect, you can ignore my first paragraph above.

Also, the id you currently have ( tr_1#?#1 ) might work, but it's really a bad id -- the # character may well cause problems, plus it's just not very descriptive. I would strongly suggest picking meaningful ids.

Try this,

   var row = document.getElementById("tr_1#?#1");
   var x = row.insertCell(0);
   x.innerHTML="New cell";

I got this code from here

Fiddle

$('#trId').prepend('<td>My new table cell</td>');

您可以使用appendTo将TD附加到TR

$('<td>My new table cell</td>').appendTo('#trId');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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