简体   繁体   中英

How to populate html table using javascript without using div?

I am trying to fill html table(my table got 3 columns) with data from json using javascript but the data never get filled using div method! could any tell me how to fill content of table rows using without using div ?

 for(i in json)
    {


    var div = "<tr id=\""+i+"\">\n" +
    "<td>"+i+"</td>\n" +
    "<td><img src=\""+ json[i].thumb +"\" height=\"42\" width=\"42\"></td>\n" +
    "<td>\n" +
    "<a href=\"javascript:callfunction('Name=" + json[i].title + "','" + json[i].sources + "','"+ json[i].thumb +"')\" onclick=\"selectLink(this);\">" + json[i].title + "</a><br> \n" +
    "<br></td></tr>\n\n";

    $("#myDiv").append(div);

        }

Table to be filled:

   <table id="list" cellspacing="0" border="1">

    <tr>
    <th>Item#</th>
    <th>Logo</th>
    <th>Description</th>

    </tr>


    <div id='myDiv'></div>


    </table>

This Your new table:

<table id="list" cellspacing="0" border="1">
  <thead>
    <tr>
      <th>Item#</th>
      <th>Logo</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>

  </tbody>
</table>

And code:

var html = '';

for(var i in json)
{
    html += '<tr id="'+i+'">'; 
    html += '<td>'+i+'</td>'; 
    html += '<td><img src="'+json[i].thumb+'" height="42" width="42"></td>';
    html += "<td>";
    html += "<a href=\"javascript:callfunction('Name=" + json[i].title + "','" + json[i].sources + "','"+ json[i].thumb +"')\" onclick=\"selectLink(this);\">" + json[i].title + "</a><br/><br/>"; 
    html += '</td>';
    html += '</tr>';
}

$('#list > tbody').html(html);

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