JavaScript HTML테이블 - 위키테이블 변환

by JM note

HTML

<style>
th, td, pre { border: 1px solid #000; }
</style>
<table id='mytable'>
  <tr><th>#</th><th>Num</th></tr>
  <tr><td>1</td><td>One</td></tr>
  <tr><td>2</td><td>Two</td></tr>
</table>
<hr>
<pre id='result'></pre>

<script src="//code.jquery.com/jquery.min.js"></script>
<script>
var wiki="{| class='wikitable'\n";
$('#mytable').each(function() {
  $(this).find('tr').each(function () {
    wiki += '|-\n';
    $(this).find('th').each(function () {
      wiki += '! ' + $(this).text() + ' ';
    });
    $(this).find('td').each(function () {
      wiki += '| ' + $(this).text() + ' ';
    });
    wiki += "\n";
  });
});
wiki += "|}"
$('#result').html(wiki);
</script>