首页
学习
活动
专区
圈层
工具
发布

数据表
EN

Stack Overflow用户
提问于 2014-01-13 07:11:29
回答 1查看 159关注 0票数 0

基本上,我需要帮助使用"td“和"tr”标记在表中显示数据。

代码语言:javascript
复制
html += '<table class="table table-striped">';
for(var i in value){
    if(!number_regex.test(i)){
        html += '<tr>';
        html += '<td>' + i + '</td>';
        if(i == 'image_link'){
            html += '<td> : ' + '<img src=' + value.image_link + ' alt>' + '</td>';
        }
        else{
            html += '<td> : ' + value[i] + '</td>';
        }
        html += '</tr>';
    }
    }
    html += '</table><br/>';

这是我当前的代码,其中的值是一个对象。这将在每个"tr“中输出一个"td”。我试着在一个"tr“中对齐2”td“。但是,这不能通过next(i)或(i+1)完成,因为它不是一个数字。

到目前为止,我一直在努力做到这一点,但没有结果:

代码语言:javascript
复制
html += '<td>' + next(i) + '</td>';
if(next(i) == 'image_link'){
    html += '<td> : ' + '<img src=' + value.image_link + ' alt>' + '</td>';
}
else{
    html += '<td> : ' + value[next(i)] + '</td>';
}

以及:

代码语言:javascript
复制
html += '<table class="table table-striped">';
html += '<tr>';
for(var i in value){
    if(!number_regex.test(i)){
        html += '<td>' + i + '</td>';
        if(i == 'image_link'){
            html += '<td> : ' + '<img src=' + value.image_link + ' alt>' + '</td>';
        }
        else{
            html += '<td> : ' + value[i] + '</td>';
            console.log(data);
        }
        for(var counter = 0; counter <= 8; counter++){
            //8 because there are 9 fields in the query I want to display
            if(counter % 2 == '0'){ 
                html += '</tr>';
                html += '<tr>';
            }
        }
    }

伙计们有什么想法吗?这些是你们中的一些人要求的附加信息:我有atm:https://www.dropbox.com/s/kmhkwturtmcxovh/lala.png表的图片,至于html,这是javascript语法,这就是为什么我以这种方式使用html的原因。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-13 08:09:22

接受一个变量并将其放入循环中,循环通过添加</tr>...something来完成结束行的工作,如下所示:

这就是你现在拥有的:

代码语言:javascript
复制
   html += '</tr>';

修改如下:

代码语言:javascript
复制
$var = 0; /* initiaize it somewhere outside for(var i in value) */

if(++$var < 2)
{
     /* do not echo </tr> if 2 td's haven't been added */
}
else /* 2 td's have been added, end this row */
{
 $var = 0; /* reset counter*/ 
 html += '</tr>'; /* end rows */
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21085721

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档