亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

javascript中使用正則表達(dá)式清理table樣式的代碼

 更新時(shí)間:2020年04月01日 23:50:44   投稿:hebedich  
本文給大家講解的是使用javascript實(shí)現(xiàn)去除多余的TABLE的樣式,主要通過結(jié)合正則表達(dá)式來實(shí)現(xiàn),非常的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。

項(xiàng)目中遇到這樣的需求,一大段文章正文的html代碼在手機(jī)中顯示不全,原因是由于其它有table,而table表格中的tr/td都攜帶了從word中粘貼過來的樣式,需要將這一大段的字符串中的table、tr、td中攜帶的樣式清除掉,同時(shí)還不能破壞table結(jié)構(gòu),即要保留tr中的rowspan和td中的colspan屬性。

html部分代碼如下:

<p class="MsoNormal" align="left" style="text-align:left"><span lang="EN-US">
 <o:p>文字中華人民共和國(guó)文字中華人民共和國(guó)文字中華人民共和國(guó)</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">項(xiàng)目<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">經(jīng)辦人<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">是否有發(fā)票<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">合計(jì)<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">文字中華人民共和國(guó)文字中華人民共和國(guó)文字中華人民共和國(guó)。</span><span lang="EN-US" style="color:#1F497D">
 <o:p></o:p>
 </span></p>

JS腳本如下:

/*
 *格式化內(nèi)容,str即是html格式的字符串
 */
function formatContent(str){
 str=str.replace(/<\/?(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]+)="([^"]+)?"/ig,function(c,d,e){
  return d === 'rowspan' ? (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]+)="([^"]+)?"/ig,function(c,d,e){
  return d === 'colspan' ? (d + '="' + e + '"') : '';
  })
  return a;
 }else{
  return '<td>';
 }
 });
 return str;
}

腳本之家小編再給大家推薦一個(gè)

//表格替換 
str=str.replace(/<table[^<>]*>/ig, "<table>");
str=str.replace(/<thead[^<>]*>/ig, "<thead>");
str=str.replace(/<tbody[^<>]*>/ig, "<tbody>");
str=str.replace(/<tfoot[^<>]*>/ig, "<tfoot>");
str=str.replace(/<tr[^<>]*>/ig, "<tr>");
str=str.replace(/<th [^<>]*>/ig, "<th>");
str=str.replace(/<td[^<>]*>/ig, "<td>");
str=str.replace(/<th>\s*?<p>/ig, "<th>");
str=str.replace(/<\/p>\s*?<\/th>/ig, "</th>");
str=str.replace(/<td[^<>]*>\s*?<p>/ig, "<td>");
str=str.replace(/<td>\s*?<p>/ig, "<td>");
str=str.replace(/<\/p>\s*?<\/td>/ig, "</td>");

這樣對(duì)于表格中所有出現(xiàn)的標(biāo)簽都進(jìn)行了替換,因?yàn)楝F(xiàn)在都是用css控制的,基本上不用這么多參數(shù)什么的了,除非特殊的表格

以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。

相關(guān)文章

  • JavaScript使用pop方法移除數(shù)組最后一個(gè)元素用法實(shí)例

    JavaScript使用pop方法移除數(shù)組最后一個(gè)元素用法實(shí)例

    這篇文章主要介紹了JavaScript使用pop方法移除數(shù)組最后一個(gè)元素,實(shí)例分析了javascript中pop方法的使用技巧,需要的朋友可以參考下
    2015-04-04
  • 比較JavaScript對(duì)象的四種方式

    比較JavaScript對(duì)象的四種方式

    這篇文章主要介紹了比較 JavaScript 對(duì)象的四種方式,對(duì)js對(duì)象感興趣的同學(xué),可以參考下
    2021-04-04
  • 簡(jiǎn)單談?wù)凟S6的六個(gè)小特性

    簡(jiǎn)單談?wù)凟S6的六個(gè)小特性

    ES6(ECMAScript2015)的出現(xiàn),無疑給前端開發(fā)人員帶來了新的驚喜,它包含了一些很棒的新特性,可以更加方便的實(shí)現(xiàn)很多復(fù)雜的操作,提高開發(fā)人員的效率。JS社區(qū)的每個(gè)人都喜歡新的API、語法以及一些簡(jiǎn)單、明了更高效的完成重要任務(wù)的新特性。下面來一起看看吧。
    2016-11-11
  • JavaScript編寫猜拳游戲

    JavaScript編寫猜拳游戲

    這篇文章主要為大家詳細(xì)介紹了JavaScript編寫猜拳游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • JavaScript實(shí)現(xiàn)的in_array函數(shù)

    JavaScript實(shí)現(xiàn)的in_array函數(shù)

    這篇文章主要介紹了JavaScript實(shí)現(xiàn)的in_array函數(shù),用于判斷一個(gè)值是否在數(shù)組中,類似PHP的in_array函數(shù),需要的朋友可以參考下
    2014-08-08
  • JavaScript實(shí)現(xiàn)網(wǎng)頁端播放攝像頭實(shí)時(shí)畫面

    JavaScript實(shí)現(xiàn)網(wǎng)頁端播放攝像頭實(shí)時(shí)畫面

    這篇文章主要介紹了如何利用JavaScript實(shí)現(xiàn)在網(wǎng)頁端播放局域網(wǎng)(不能上云)或是廣域網(wǎng)的攝像頭的實(shí)時(shí)畫面,文中的示例代碼講解詳細(xì),需要的可以參考一下
    2022-02-02
  • 基于JavaScript介紹性能爆表的SolidJS

    基于JavaScript介紹性能爆表的SolidJS

    這篇文章主要介紹了基于JavaScript介紹性能爆表的SolidJS,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-09-09
  • 原生JS與jQuery編寫簡(jiǎn)單選項(xiàng)卡

    原生JS與jQuery編寫簡(jiǎn)單選項(xiàng)卡

    這篇文章主要為大家詳細(xì)介紹了原生JS與jQuery編寫簡(jiǎn)單選項(xiàng)卡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • 深入理解 ES6中的 Reflect用法

    深入理解 ES6中的 Reflect用法

    這篇文章主要介紹了深入理解 ES6中的 Reflect用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • Javascript Ajax異步讀取RSS文檔具體實(shí)現(xiàn)

    Javascript Ajax異步讀取RSS文檔具體實(shí)現(xiàn)

    這篇文章主要介紹了Javascript Ajax異步讀取RSS文檔具體實(shí)現(xiàn),有需要的朋友可以參考一下
    2013-12-12

最新評(píng)論