$(function () { if ($('.post-body h1, .post-body h2, .post-body h3, .post-body h4').length) { var topicHtml = ''; $('.post-body h1, .post-body h2, .post-body h3, .post-body h4').each(function (index, obj) { $(obj).attr('id', 'point-' + index); if ($(obj).prop('tagName') == 'H2') { topicHtml += '  '; } else if ($(obj).prop('tagName') == 'H3') { topicHtml += '    '; } else if ($(obj).prop('tagName') == 'H4') { topicHtml += '      '; } var cat = $(obj).text(); if ($(obj).prop('tagName') == 'H2') { topicHtml += '' + (index + 1) + '.' + cat + '
'; } if ($(obj).prop('tagName') == 'H3') { topicHtml += '' + cat + '
'; } }); $('#topic').html(topicHtml); } });