Home > Web Front-end > JS Tutorial > javascript clean table style_javascript tips

javascript clean table style_javascript tips

WBOY
Release: 2016-05-16 15:50:40
Original
1604 people have browsed it

I encountered such a demand in the project. The html code of a large article text was not fully displayed on the mobile phone. The reason was that there was a table, and the tr/td in the table table all carried styles pasted from word. , it is necessary to clear the styles carried in table, tr, and td in this large string, and at the same time, the table structure cannot be destroyed, that is, the rowspan in tr and the colspan in td must be retained.

The html part of the code is as follows:

<p class="MsoNormal" align="left" style="text-align:left"><span lang="EN-US">
 <o:p>文字中华人民共和国文字中华人民共和国文字中华人民共和国</o:p>
 </span></p>
<table>
 <tbody>
  <tr style="height:13.5pt">
   <td width="117" style="width:88.0pt;border:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:13.5pt"><p class="MsoNormal" align="center" style="text-align:center"><span style="font-family:宋体;color:#1F497D">项目<span lang="EN-US">
     <o:p></o:p>
     </span></span></p></td>
   <td width="137" style="width:103.0pt;border:solid windowtext 1.0pt;border-left:none;padding:0cm 5.4pt 0cm 5.4pt;height:13.5pt"><p class="MsoNormal" align="center" style="text-align:center"><span style="font-family:宋体;color:#1F497D">金额<span lang="EN-US">
     <o:p></o:p>
     </span></span></p></td>
   <td width="153" style="width:115.0pt;border:solid windowtext 1.0pt;border-left:none;padding:0cm 5.4pt 0cm 5.4pt;height:13.5pt"><p class="MsoNormal" align="center" style="text-align:center"><span style="font-family:宋体;color:#1F497D">经办人<span lang="EN-US">
     <o:p></o:p>
     </span></span></p></td>
   <td width="135" style="width:101.0pt;border:solid windowtext 1.0pt;border-left:none;padding:0cm 5.4pt 0cm 5.4pt;height:13.5pt"><p class="MsoNormal" align="center" style="text-align:center"><span style="font-family:宋体;color:#1F497D">是否有发票<span lang="EN-US">
     <o:p></o:p>
     </span></span></p></td>
  </tr>
  <tr style="height:13.5pt">
   <td width="117" style="width:88.0pt;border:solid windowtext 1.0pt;border-top:none;padding:0cm 5.4pt 0cm 5.4pt;height:13.5pt"><p class="MsoNormal" align="center" style="text-align:center"><span style="font-family:宋体;color:#1F497D">合计<span lang="EN-US">
     <o:p></o:p>
     </span></span></p></td>
   <td colspan="3" valign="bottom" nowrap="" style="width:103.0pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:13.5pt"><p class="MsoNormal" align="center" style="text-align:center"><span lang="EN-US" style="font-size:11.0pt;font-family:宋体;color:black">
     <o:p></o:p>
     </span></p></td>
  </tr>
 </tbody>
</table>
<p class="MsoNormal"><span style="font-family:宋体;color:#1F497D">文字中华人民共和国文字中华人民共和国文字中华人民共和国。</span><span lang="EN-US" style="color:#1F497D">
 <o:p></o:p>
 </span></p>
Copy after login

The JS script is as follows:

/*
 *格式化内容,str即是html格式的字符串
 */
function formatContent(str){
  str=str.replace(/<\/&#63;(html|head|title|meta|body)\b[^>]*>/ig,"");
  str=str.replace(/<table[^>]*>/ig,"<table>");
  return str;
  str=str.replace(/(<tr[^>]*>)/ig, function (a, b) {
    if(a.indexOf('rowspan')>-1){
      a=a.replace(/([a-z]+)="([^"]+)&#63;"/ig,function(c,d,e){
        return d === 'rowspan' &#63; (d + '="' + e + '"') : '';
      })
      return a;
    }else{
      return '<tr>';
    }
  });
  str=str.replace(/(<td[^>]*>)/ig, function (a, b) {
    if(a.indexOf('colspan')>-1){
      a=a.replace(/([a-z]+)="([^"]+)&#63;"/ig,function(c,d,e){
        return d === 'colspan' &#63; (d + '="' + e + '"') : '';
      })
      return a;
    }else{
      return '<td>';
    }
  });
  return str;
}
Copy after login

The above is the entire content of this article, I hope you all like it.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template