[英]How to convert/transform in the DOM following dl dt structure to table on the fly using jQuery or d3js in a document
如何在文档中使用 jQuery 或 d3js 在 DOM 中根据 dl dt 值动态转换为表格 行可能具有任何数据元素顺序和一些缺失列......感觉就像需要将操作行转置为列
从
<div id="conatainer">
<dl class="table">
<dt class="row">1</dt>
<dd>
<dl>
<dt>name</dt>
<dd>Tom Cruise</dd>
<dt>age</dt>
<dd>56</dd>
</dl>
</dd>
<dt class="row">2</dt>
<dd>
<dl>
<dt>dob</dt>
<dd>April 4, 1965</dd>
<dt>name</dt>
<dd>Robert Downey Jr.</dd>
<dt>age</dt>
<dd>53</dd>
</dl>
</dd>
</dl>
</div>
至
<div id="conatainer">
<table>
<thead>
<tr>
<th>s.no</th>
<th>name</th>
<th>age</th>
<th>dob</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Tom Cruise</td>
<td>56</td>
<td> </td>
</tr>
<tr>
<td>2</td>
<td>Robert Downey Jr.</td>
<td>53</td>
<td>April 4, 1965</td>
</tr>
</tbody>
</table>
</div>
为表设置一个 ID 并最初隐藏它,然后使用replaceWith
$('button').click(() => { const newTable = $('#new-table'); newTable.show(); $('dl').replaceWith(newTable); });
dl{ background: green } table{ background: blue; display: none }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button>replace</button> <dl class="table"> <dt class="row">1</dt> <dd> <dl> <dt>name</dt> <dd>Tom Cruise</dd> <dt>age</dt> <dd>56</dd> </dl> </dd> <dt class="row">2</dt> <dd> <dl> <dt>dob</dt> <dd>April 4, 1965</dd> <dt>name</dt> <dd>Robert Downey Jr.</dd> <dt>age</dt> <dd>53</dd> </dl> </dd> </dl> <table id='new-table'> <thead> <tr> <th>s.no</th> <th>name</th> <th>age</th> <th>dob</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>Tom Cruise</td> <td>56</td> <td></td> </tr> <tr> <td>2</td> <td>Robert Downey Jr.</td> <td>53</td> <td>April 4, 1965</td> </tr> </tbody> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.