首页 新闻 会员 周边 捐助

用JS怎么样才能把一个用html()获得的table简化

0
悬赏园豆:10 [已解决问题] 解决于 2017-12-29 13:08

用JS的html()获取到的table有原来的属性,样式啥的,

怎么样把这个字符串弄成只有table最基本结构的字符串,把属性,空格和CSS全都剔除?

本来无意间找到了,但是没在意,现在好后悔,好像是用正则处理一下就行了,

关键是正则的表达式不知道怎么弄,

求大神帮忙

比如:

<table class="....." style="....." id="......">
<tr style="......" role="row" rowspan="1" colspan="1">
   <th></th>
</tr>
<tr style="......" role="row" rowspan="1" colspan="1">
   <td></td>
</tr>
</table>

简化成最基本的<table><tr><th></th></tr></trable>

Anmen的主页 Anmen | 初学一级 | 园豆:70
提问于:2017-12-25 13:26
< >
分享
最佳答案
0

 

var str = "****";

str = str.replace(/\s/g. "");

str.replace(/(<table|<tr|<th|<td).*?(>)/g, $1$2);

 

收获园豆:5
小明1992 | 菜鸟二级 |园豆:236 | 2017-12-29 01:18
其他回答(3)
0

把你的所有代码贴出来吧。大家在你的基础上分析。

David5201 | 园豆:534 (小虾三级) | 2017-12-25 13:33
0

麻烦你给个样板:
原字符串:_____
目标字符串:____

BUTTERAPPLE | 园豆:3190 (老鸟四级) | 2017-12-25 13:33
0
  1 <html>
  2 
  3 <head>
  4     <link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.4/css/bootstrap.css">
  5     <script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
  6     <script src="http://apps.bdimg.com/libs/bootstrap/3.3.4/js/bootstrap.js"></script>
  7 </head>
  8 
  9 <body>
 10 
 11     <div id="src">
 12         <table class="table">
 13             <thead>
 14                 <tr>
 15                     <th>#</th>
 16                     <th>Column heading</th>
 17                     <th>Column heading</th>
 18                     <th>Column heading</th>
 19                 </tr>
 20             </thead>
 21             <tbody>
 22                 <tr class="active">
 23                     <th scope="row">1</th>
 24                     <td>Column content</td>
 25                     <td>Column content</td>
 26                     <td>Column content</td>
 27                 </tr>
 28                 <tr>
 29                     <th scope="row">2</th>
 30                     <td>Column content</td>
 31                     <td>Column content</td>
 32                     <td>Column content</td>
 33                 </tr>
 34                 <tr class="success">
 35                     <th scope="row">3</th>
 36                     <td>Column content</td>
 37                     <td>Column content</td>
 38                     <td>Column content</td>
 39                 </tr>
 40                 <tr>
 41                     <th scope="row">4</th>
 42                     <td>Column content</td>
 43                     <td>Column content</td>
 44                     <td>Column content</td>
 45                 </tr>
 46                 <tr class="info">
 47                     <th scope="row">5</th>
 48                     <td>Column content</td>
 49                     <td>Column content</td>
 50                     <td>Column content</td>
 51                 </tr>
 52                 <tr>
 53                     <th scope="row">6</th>
 54                     <td>Column content</td>
 55                     <td>Column content</td>
 56                     <td>Column content</td>
 57                 </tr>
 58                 <tr class="warning">
 59                     <th scope="row">7</th>
 60                     <td>Column content</td>
 61                     <td>Column content</td>
 62                     <td>Column content</td>
 63                 </tr>
 64                 <tr>
 65                     <th scope="row">8</th>
 66                     <td>Column content</td>
 67                     <td>Column content</td>
 68                     <td>Column content</td>
 69                 </tr>
 70                 <tr class="danger">
 71                     <th scope="row">9</th>
 72                     <td>Column content</td>
 73                     <td>Column content</td>
 74                     <td>Column content</td>
 75                 </tr>
 76             </tbody>
 77         </table>
 78     </div>
 79     <div id="result">
 80 
 81     </div>
 82     <script>
 83         $(function() {
 84             var rmAllAttr = function(self, Recursive, tagfliter) {
 85                 self.each(function() {
 86                     if (tagfliter.length == 0 || tagfliter.indexOf(this.tagName.toLowerCase()) < 0) return;
 87                     var attrs = this.attributes;
 88                     console.log(attrs)
 89                     for (i in attrs) {
 90                         $(this).removeAttr(attrs[i].name);
 91                     }
 92                     if (Recursive) rmAllAttr($(this).children(tagfliter.join()), Recursive, tagfliter)
 93                 })
 94             }
 95 
 96             var html = $("#src").clone();
 97             var tags = ['table', 'thead', 'tbody', 'tr', 'td', 'th'];
 98 
 99             rmAllAttr(html.children(), true, tags);
100             $("#result").html(html.html());
101 
102 
103 
104         })
105     </script>
106 </body>
107 
108 </html>

 

偷懒写了递归,虽然不怎么科学,但是还挺好用

收获园豆:5
写代码的相声演员 | 园豆:517 (小虾三级) | 2017-12-26 16:32
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册