我最近需要做同样的事情。和你一样,我希望在交换周围的 html 时保留内容。CSS 主要用于设置表格/网格的样式。这里真正的明星是 jQuery。
看看:http:
//jsfiddle.net/rymill2/R3J5m/
HTML
<div class="row">
<a class="button-table" data-target="table"><img src="http://fakeimg.pl/100x50/282828/fff/?text=Table"></a>
<a class="button-grid" data-target="grid"><img src="http://fakeimg.pl/100x50/282828/fff/?text=Grid"></a>
</div>
<div class="content">
<table>
<tr class="table-head">
<th>Title</th>
<th>Date</th>
<th>Info</th>
</tr>
<tr class="result">
<div class="settings"></div> <a class="link" href=""></a>
<td class="title">Class Title</td>
<td class="row">Item Details</td>
<td class="row">Item Details</td>
</tr>
<tr class="result">
<div class="settings"></div> <a class="link" href=""></a>
<td class="title">Class Title</td>
<td class="row">Item Details</td>
<td class="row">Item Details</td>
</tr>
<tr class="result">
<div class="settings"></div> <a class="link" href=""></a>
<td class="title">Class Title</td>
<td class="row">Item Details</td>
<td class="row">Item Details</td>
</tr>
<tr class="result">
<div class="settings"></div> <a class="link" href=""></a>
<td class="title">Class Title</td>
<td class="row">Item Details</td>
<td class="row">Item Details</td>
</tr>
</table>
</div>
CSS
.table-head, div, a, p, span {
box-sizing:border-box !important;
}
.columns, .column {
float:left;
position:relative;
}
.h-100 {
min-height:110px;
}
.three {
width:25%;
}
a:hover {
cursor:pointer;
background:#e9e9e9;
}
a.toggle-table, a.toggle-grid {
position:relative;
float:left;
margin-right:6px;
font-size:20px;
padding:8px;
}
.settings, .link {
display:none;
}
.grid {
overflow:auto;
}
.grid .result {
padding:2%;
background:#e9e9e9;
border:1px solid #333;
}
.grid .result:first-child {
display:none;
}
.grid .result .title, .grid.result .row, .grid .result .link {
position:relative;
float:left;
width:100%;
}
.grid .result .title {
border-bottom:1px solid yellow;
padding-bottom:2px;
margin-bottom:2px;
color:#282828;
font-size:16px;
font-weight:bold;
}
.grid .result .row {
color:#666;
font-size:10px;
}
.table-head {
background:#333;
color:#fff;
text-align:left;
font-size:12px;
}
JS
$('.button-table').click(function() {
$('.grid').replaceWith(function() {
var html = '';
$('div:first', this).each(function() {
html += '<tr class="table-head">';
$('div', this).each(function() {
html += '<th>' + $(this).html() + '</th>';
});
html += '</tr>';
});
$('div:not(:first)', this).each(function() {
var innerHtml = '';
$('div', this).each(function() {
innerHtml += '<td>' + $(this).html() + '</td>';
});
if (innerHtml != '') {
html += '<tr>' + innerHtml + '</tr>';
}
});
return '<table>' + html + '</table>';
});
});
$('.button-grid').click(function() {
$('table').replaceWith(function() {
var html = '';
$('tr', this).each(function() {
html += '<div class="result three columns h-100">';
$('th', this).each(function() {
html += '<div>' + $(this).html() + '</div>';
});
$('td', this).each(function() {
html += '<div>' + $(this).html() + '</div>';
});
html += '</div>';
});
return '<div class="grid">' + html + '</div>';
});
});