简体   繁体   English

Jquery 附加表

[英]Jquery Append Table

html file html文件

<div id='tweetPost'>
    <table id="example">
        <thead>
            <tr>
                <th>No</th>
                <th>FistName</th>
            </tr>
        </thead>

        <tbody></tbody>

    </table>
</div>

JavaScript JavaScript

$("#tweetPost").append(<tr>); 

$("#tweetPost").append("<td>"+tweets.statuses[i].text + "<td/>");
$("#tweetPost").append("<td>"+tweets.statuses[i].created_at +"</td>");

$("#tweetPost").append(</tr>); 

Above code when i try to run it , the table wont come out.当我尝试运行上面的代码时,表格不会出来。

Question : How can i append the td row inside tbody??问题:如何在 tbody 中附加 td 行?

 $('#tweetPost').append('<table></table>'); var table = $('#tweetPost').children(); table.append("<tr><td>a</td><td>b</td></tr>"); table.append("<tr><td>c</td><td>d</td></tr>");
 table { background: #CCC; border: 1px solid #000; } table td { padding: 15px; border: 1px solid #DDD; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='tweetPost'></div>

Note:- You can tackle your table id & the tbody注意:- 您可以处理您的表idtbody

You should try targeting your table id example and the tbody like so:您应该尝试像这样定位您的 table id 示例和 tbody:

$("#example tbody").append("<tr><td>text</td><td>created</td></tr>");

See this link for a working example: append to example table有关工作示例,请参阅此链接:附加到示例表

You are appending the tr in div instead of tbody and the is also some syntax error.您在div而不是tbody中附加了tr并且这也是一些语法错误。 Try like following.尝试如下。

$("#example tbody").append("<tr><td>" + tweets.statuses[i].text + "<td/><td>" + tweets.statuses[i].created_at + "</td><tr>");

You've missed inverted comma " " in first and last lines.您在第一行和最后一行遗漏了引号“” Try this:试试这个:

$("#tweetPost").append("<tr>"); 

$("#tweetPost").append("<td>"+tweets.statuses[i].text + "<td/>");
$("#tweetPost").append("<td>"+tweets.statuses[i].created_at +"</td>");

$("#tweetPost").append("</tr>");

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

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