简体   繁体   中英

Jquery Append Table

html file

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

        <tbody></tbody>

    </table>
</div>

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??

 $('#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

You should try targeting your table id example and the tbody like so:

$("#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. 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>");

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