Pri*_*ank 3 html javascript css jquery
我正在使用下面的代码将表结构化数据转换为div.
$('#content').html($('#content').html()
.replace(/<tbody/gi, "<div id='table'")
.replace(/<tr/gi, "<div style='overflow:auto;padding-top:15px;'")
.replace(/<\/tr>/gi, "</div>")
.replace(/<td/gi, "<span style='float:left;margin-right:20px;'")
.replace(/<\/td>/gi, "</span>")
.replace(/<\/tbody/gi, "<\/div"));
Run Code Online (Sandbox Code Playgroud)
它除了ROWSPAN和COLSPAN案例外,主要适用于所有场景.
在将Table转换为Div时如何处理该设计问题?我陷入了困境.
也许这会让你朝着正确的方向前进:
.replace(/rowspan="/gi, 'class="rowspan-')
.replace(/colspan="/gi, 'class="colspan-')
Run Code Online (Sandbox Code Playgroud)
然后为类创建样式(例如rowspan-2或colspan-3等).但是,这并不能解决一个元素同时具有row-and colspan的情况,但这只是一个开始.
更好的方法是:
var copyAttr = function(old, $new) {
for(var i = 0,
attributes = old.attributes;
i < attributes.length; i++) {
$new.attr(attributes[i].name, attributes[i].value);
}
return $new;
}
$('#content').find('tbody').each(function() {
var $new = copyAttr(this, $('<div id="table"></div>');
$(this).replaceWith($new);
}).end().find('tr').each(function() {
var $new = copyAttr(this, $('<div class="tr"></div>');
$(this).replaceWith($new);
}).end().find('td').each(function() {
var $new = copyAttr(this, $('<span class="td"></span>');
$(this).replaceWith($new);
});
Run Code Online (Sandbox Code Playgroud)
所以现在你用div和span替换了整个表结构,其中包含了table元素的所有属性.接下来,您可以将row-and colspan属性更改为类.
$('#table .td').each(function() {
var $t = $(this);
$t
.addClass('rowspan-'+$t.attr('rowspan'))
.removeAttr('rowspan')
.addClass('colspan-'+$t.attr('colspan'))
.removeAttr('colspan');
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3205 次 |
| 最近记录: |