简体   繁体   English

Jquery添加 <td> 到表 <tr id>

[英]Jquery add <td> to table by <tr id>

Can i do that using jquery? 我可以使用jquery吗? Unfortunately i don't have a table id, only a tr id.. 不幸的是我没有表id,只有一个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. 当您已经有结束标记时,您尝试通过仅注入开始标记来修改DOM。 You can't do that. 你不能这样做。 First, your existing HTML is invalid. 首先,您现有的HTML无效。 Second, that's not how DOM manipulation works. 其次,这不是DOM操作的工作方式。

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> . 我正在读这个意思是你已经</td>并希望将<td ...>something放在你目前<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. 此外,您当前拥有的ID( tr_1#?#1 )可能有效,但它确实是一个糟糕的ID - #字符可能会导致问题,而且它只是描述性不强。 I would strongly suggest picking meaningful ids. 我强烈建议选择有意义的ID。

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');

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

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