html - 按列而不是行填充 html 表?

标签 html css html-table

'<table> 
    <tr>
        <th>' .$varProduct[$x][0].  '</th>'.  //this is a title
       '<th>' .$varProduct[$x][2] . $varProduct[$x][11] . ' </th> // this is a price
    </tr>
</table>'   

这段代码制作了一个如下所示的表格:

Title Price
Title Price
Title Price

但我想填一下

Title Title Title
Price Price Price

我可以使用一些库或 CSS 样式来做到这一点吗?我知道这不是 DOM 通常对齐元素的方式。

好的,现在是这样

Title Title Title Title   //uses up the whole page so goes to next linke
Title Price Price PRice 
PRice

我愿意去

Title Title Title TItle
PRice PRice PRice PRice
Title 
PRice

最佳答案

已更新 fiddle因此,但使用 JS。它应该给你一个想法。

HTML

<div id="dynamic">

</div>

JS

var priceList = [ 'price1', 'price2', 'price3'];
var itemList = [ 'item1', 'item2', 'item3' ];

$('#dynamic').append('<table><caption>DYNAMIC</caption><tr>');   
for (var i=0; i<3; i++){

    $('#dynamic').append('<td>' + itemList[i] + '</td>');
}
$('#dynamic').append('</tr><tr>');

for (var i=0; i<3; i++){

    $('#dynamic').append('<td>' + priceList[i] + '</td>');
}
$('#dynamic').append('</tr></table>');

更新

更新输出

DYNAMIC
item1   item2   item3   item4
price1  price2  price3

查看 updated fiddle

关于html - 按列而不是行填充 html 表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30989927/

相关文章:

html - CSS:为什么我的图像没有被渲染?

jQuery:如何将复选框 append 到表中的每一行?

c# - 即时格式化 html 日期和数字

javascript - JavaScript 中带有欧拉 Angular 的 CSS 立方体全景控件

javascript - 二维变换 - 如何保持位置?

javascript - CSS 中是否有最小可能的尺寸变化?

javascript - 选择新 tr 后将所选 tr 颜色改回原样

html - Firefox 中折叠的表格边框有时会丢失

css - 即使位置设置为相对且不透明度设置为 0.99,z-index 也无效

javascript - 如何使用 ReactTestUtils 设置 HTML5 文件类型输入