jquery - 循环中生成的回流表无响应

标签 jquery html css jquery-mobile html-table

ajax 成功 后,我在 jQuery mobile 中循环生成一个回流表。但是问题是表格没有响应。在小屏幕上,生成的表格应该通过将表格列折叠成堆叠的表示来工作,看起来像每一行的标签/数据对 block 。当我输出从循环生成的表标记并将其复制/粘贴到单独的页面时,该表看起来很好并且响应迅速。

我生成的html是:

<table data-role="table" id="time-table" data-mode="reflow" class="ui-responsive table-stroke">
    <thead>
       <tr>
         <th>Linje</th>
         <th>Destination</th>
         <th>Nästa tur (min)</th>
         <th>Därefter</th>
       </tr>
    </thead>
    <tbody>
       <tr>
         <th>7</th>
         <td>Resecentrum</td>
         <td>Nu</td>
         <td>11</td>
       </tr>
       <tr>
         <th>7</th>
         <td>Ö Lugnet via Resecentrum</td>
         <td>23</td>
         <td>51</td>
       </tr>
    </tbody>
</table>

在小屏幕上看起来像这样当我复制生成的标记并粘贴到一个单独的文件中这就是我需要的

enter image description here

但是,当我使用代码动态执行时:

success: function(data){
    //Generate table header and populate.
var thdata1 = "<th>"+data[2]+"</th>";
var thdata2 = "<th>"+data[3]+"</th>";
var thdata3 = "<th>"+data[4]+"</th>";
var thdata4 = "<th>"+data[6]+"</th>";

    var tblrow = $("<tr></tr>");
var thead = $("<thead></thead>");
var table = $("<table data-role=\"table\" id=\"time-table\" data-mode=\"reflow\" class=\"ui-responsive table-stroke\">");

tblrow.append(thdata1);
tblrow.append(thdata2);
tblrow.append(thdata3);
tblrow.append(thdata4);

    thead.append(tblrow);
table.append(thead)

    //Generate table body and populate.
var row = $("<tr>");
var flag = 0;
var tbody = $('<tbody>');
$.each(data, function(key, val){
    if(key >= 8){
    if(key%4 != 3){
      if(flag == 0)
        row.append("<th>"+val+"</th>");
      else
        row.append("<td>"+val+"</td>");
      flag++;
    }
    else if(key%4 == 3){
      row.append("<td>"+val+"</td>");
      tbody.append(row);
      row = $("<tr>");
      flag = 0;
    }
     }
     });
   table.append(tbody);
   table.appendTo("#contbl");
   console.log($("#contbl").html());
}

它生成一个正常布局的无响应表格,如下图所示,即使在较小的屏幕上也保持相同的结构。此外,CSS 在此布局中看起来不合适。我正在使用 jQM 提供的默认表 class="ui-responsive table-stroke"

enter image description here

我生成的标记工作正常。我不知道这里有什么问题。

最佳答案

当您像您的情况一样动态创建 html 时,您必须使用

更新页面内容
$('page_id').trigger('pagecreate');

关于jquery - 循环中生成的回流表无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17807961/

相关文章:

javascript - PHP AJAX MySql 抓取数据 - 单选按钮

javascript - 在使用 skrollr.js 和 ID 为 "skrollr-body"的多个 div 时,Scroll 在 android 中不起作用

javascript - 如何取消选中 Framework7 中的自定义设计单选按钮?

css - 如何在 Wordpress 的同一行上获得两种字体?

javascript - 带有搜索按钮的jquery下拉框

javascript - SimpleModal 基本模态窗口在选择时显示无效页面

jquery - 在索引匹配上运行函数停止 jQuery

python - 我怎样才能用 python sub 删除 <p> </p>

javascript - jquery - 选中单选按钮时附加

html - header 和 div 出现在同一行