我想顯示我的表格數據到移動佈局的不同行中。所以我想要做的是在僞選擇器之前添加值。所以它應該像這樣顯示。加<th>每個文本- jquery
名稱:XYZ
年齡:20
名稱:ABC
年齡:30
請看到我的jQuery代碼。這是工作的罰款如果只有一個,但如果有多個然後它不工作
JQuery的:
if ($('table').length) {
var th_data;
$('table th').each(function (index) {
var index_th = index;
th_data = $(this).text();
$('table td').each(function (index) {
var index_td = index;
if (index_th == index_td) {
$(this).attr('data-content', th_data);
}
});
});
}
CSS代碼:
table th{display:none;}
table td:before{content:attr(data-content) '';position:absolute;top:6px;left:6px;width:45%;padding-right:10px;white-space:nowrap;}
table td{position:relative;padding-left:50%;display:block;}
解決方案:
我我想我已經明白了。所以更新的JQuery代碼是這樣的。它在多個工作正常
/* Changing the tabular data into rows */
if ($('table').length) {
var th_data;
$('table').each(function() {
var _table = $(this);
$(this).find('th').each(function (index) {
var index_th = index;
th_data = $(this).text();
_table.find('tr').each(function() {
$(this).find('td').each(function (index) {
var index_td = index;
if (index_th == index_td) {
$(this).attr('data-content', th_data);
}
});
});
});
});
}