【问题标题】:How to make table with DOM?如何用DOM制作表格?
【发布时间】:2015-02-14 17:15:33
【问题描述】:

此代码在 IE 中不起作用,因此我需要放置 DOM 代码而不是 INNER HTML,ths Gabriel。 所以,问题是 IE 不理解带有 TABLE、TR、TD 的 innerHTML。我需要另一种方式。但我不知道怎么做,你有链接


$(document).ready(function () {
var request = new XMLHttpRequest();
request.open('GET', 'http://api.randomuser.me/?results=11', true);

request.onreadystatechange = function() {
  if (this.readyState === 4) {
    if (this.status >= 200 && this.status < 400) {
      var resp = this.responseText;
      var yourEvent = JSON.parse(resp);
     alert(resp);
     var u = yourEvent.results[0].user;
     //alert(yourEvent.results[0].user.name.title);

     //document.getElementById("table-wrapper").innerHTML=u;

var our_html = "<table 'border='0' cellpadding='0' cellspacing='0'>";
our_html += "<tr><th style='width:52px;border-left:none;'>№</th><th>Имя Фамилия Отчество</th><th>Адрес</th><th>Фото</th><th style='width:180px;border-right: none;'>Действие</th></tr>";
    yourEvent.results.forEach(function(item, item_number){
        var u = item.user; // задрало уже длинно ))
        our_html += "<tr>";
        our_html += "<td style='width:52px;'>" + (parseInt(item_number)+1) + "</td>";
        our_html += "<td class='usname'>" + u.name.title + "." + "<span>" + u.name.first + "</span>" + " " + "<span>" + u.name.last +"</span>" + "</td>";
        our_html += "<td class='usst'>" + "<span>" + u.location.city + "</span>" + ", " + u.location.street + "</td>";
        // чтобы табличку не растаращило, на время отладки
        our_html += '<td style="width:146px;"><img style="max-width:60px; max-height:60px;border-radius: 30px;" src="'+u.picture.medium+'"></td>';
        our_html += '<td style="width:180px; border-right: solid 1px #e1e1e1;"><div class="more" onclick="alert("'+u.name.first+'")">Просмотреть</div></td>';
        our_html += "</tr>";
    });
    our_html += "</table>";

    document.getElementById("table-wrapper").innerHTML=our_html;


    } else {
      //Debug error here!
      console.error("An error occurred");
    }
  }
};
request.send();
request = null;



});

【问题讨论】:

  • 它是如何“不工作”的?你有错误吗?你试过调试吗?
  • 函数 createTable() { $("#footer").append(""); __忘记删除
  • TR , TD , TABLE 不能在 IE 中使用 INNER HTML ,我知道。所以,我需要 dom 方法
  • ths Singhakash,我会试试的

标签: javascript dom


【解决方案1】:

所以,尝试了另一个链接。这样的方法是有效的:


$('#table-wrapper').append('<table id="options"><tbody><tr><th>First</th><th>Second</th><th>Third</th><th>Fourth</th><th>Fifth</th></tr></tbody></table>');

现在我只需要放入我的变量中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-11
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多