怎样修改删除指定内容?

cde32 2015-01-28 11:35:08
把网页内容粘贴(右键或Ctrl+v)到富文本编辑器时,怎样清除table标签里的所有设定?
比如:复制网页中的<table width="1800" style="width: 1000px;">,粘贴到编辑器里变成<table>
或者简单一点:粘贴到编辑器里修改成统一的800px宽度:<table width="1800" style="width: 800px;">

...全文
317 13 打赏 收藏 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
cde32 2015-02-09
  • 打赏
  • 举报
回复
引用 6 楼 cde32 的回复:
[quote=引用 5 楼 liuxychad 的回复:] 可以用几行代码实现,代码少但是很复杂 可以等大神来解决,但你这分数也太少了点
分数不是问题,结帖可以加到一百!不够的话我开几个新帖送分。[/quote] 终于解决了! 大神绝不是为了分数!在QQ里帮我调试了好几次!分数换不来啊!
cde32 2015-02-09
  • 打赏
  • 举报
回复
引用 10 楼 xiongsisi1043 的回复:
switch ( name ) {
                            case "mso-padding-alt":
                            case "mso-padding-top-alt":
                            case "mso-padding-right-alt":
                            case "mso-padding-bottom-alt":
                            case "mso-padding-left-alt":
                            case "mso-margin-alt":
                            case "mso-margin-top-alt":
                            case "mso-margin-right-alt":
                            case "mso-margin-bottom-alt":
                            case "mso-margin-left-alt":
                            //ie下会出现挤到一起的情况
//                            case "mso-table-layout-alt":
                            case "mso-height":
                            case "mso-width":
                            case "mso-vertical-align-alt":
                                //trace:1819 ff下会解析出padding在table上
                                if(!/<table/.test(tag))
                                    n[i] = name.replace( /^mso-|-alt$/g, "" ) + ":" + ensureUnits( value );
                                continue;
                            case "horiz-align":
                                n[i] = "text-align:" + value;
                                continue;
 
                            case "vert-align":
                                n[i] = "vertical-align:" + value;
                                continue;
 
                            case "font-color":
                            case "mso-foreground":
                                n[i] = "color:" + value;
                                continue;
 
                            case "mso-background":
                            case "mso-highlight":
                                n[i] = "background:" + value;
                                continue;
 
                            case "mso-default-height":
                                n[i] = "min-height:" + ensureUnits( value );
                                continue;
 
                            case "mso-default-width":
                                n[i] = "min-width:" + ensureUnits( value );
                                continue;
 
                            case "mso-padding-between-alt":
                                n[i] = "border-collapse:separate;border-spacing:" + ensureUnits( value );
                                continue;
 
                            case "text-line-through":
                                if ( (value == "single") || (value == "double") ) {
                                    n[i] = "text-decoration:line-through";
                                }
                                continue;
 
                         
                            //trace:1870
//                            //word里边的字体统一干掉
//                            case 'font-family':
//                                continue;
                            case "mso-zero-height":
                                if ( value == "yes" ) {
                                    n[i] = "display:none";
                                }
                                continue;
                            case 'width':
if(tag ==='<table'){
 n[i]="width:800px";
}
continue;
                            case 'margin':
 
                                if ( !/[1-9]/.test( parts[1] ) ) {
                                    continue;
                                }
                            
                        }
return function ( html ) {
 
            //过了word,才能转p->li
            first = null;
            parentTag = '',liStyle = '',firstTag = '';
            //if ( isWordDocument( html ) ) {
                html = filterPasteWord( html );
            //}
            return html.replace( />[ \t\r\n]*</g, '><' );
        };
不好意思,周末不看csdn
太感谢了!我在群里问了好多次都没解决,在这里解决了!!!
cde32 2015-02-03
  • 打赏
  • 举报
回复
引用 10 楼 xiongsisi1043 的回复:
switch ( name ) {
                            case "mso-padding-alt":
                            case "mso-padding-top-alt":
                            case "mso-padding-right-alt":
                            case "mso-padding-bottom-alt":
                            case "mso-padding-left-alt":
                            case "mso-margin-alt":
                            case "mso-margin-top-alt":
                            case "mso-margin-right-alt":
                            case "mso-margin-bottom-alt":
                            case "mso-margin-left-alt":
                            //ie下会出现挤到一起的情况
//                            case "mso-table-layout-alt":
                            case "mso-height":
                            case "mso-width":
                            case "mso-vertical-align-alt":
                                //trace:1819 ff下会解析出padding在table上
                                if(!/<table/.test(tag))
                                    n[i] = name.replace( /^mso-|-alt$/g, "" ) + ":" + ensureUnits( value );
                                continue;
                            case "horiz-align":
                                n[i] = "text-align:" + value;
                                continue;
 
                            case "vert-align":
                                n[i] = "vertical-align:" + value;
                                continue;
 
                            case "font-color":
                            case "mso-foreground":
                                n[i] = "color:" + value;
                                continue;
 
                            case "mso-background":
                            case "mso-highlight":
                                n[i] = "background:" + value;
                                continue;
 
                            case "mso-default-height":
                                n[i] = "min-height:" + ensureUnits( value );
                                continue;
 
                            case "mso-default-width":
                                n[i] = "min-width:" + ensureUnits( value );
                                continue;
 
                            case "mso-padding-between-alt":
                                n[i] = "border-collapse:separate;border-spacing:" + ensureUnits( value );
                                continue;
 
                            case "text-line-through":
                                if ( (value == "single") || (value == "double") ) {
                                    n[i] = "text-decoration:line-through";
                                }
                                continue;
 
                         
                            //trace:1870
//                            //word里边的字体统一干掉
//                            case 'font-family':
//                                continue;
                            case "mso-zero-height":
                                if ( value == "yes" ) {
                                    n[i] = "display:none";
                                }
                                continue;
                            case 'width':
if(tag ==='<table'){
 n[i]="width:800px";
}
continue;
                            case 'margin':
 
                                if ( !/[1-9]/.test( parts[1] ) ) {
                                    continue;
                                }
                            
                        }
return function ( html ) {
 
            //过了word,才能转p->li
            first = null;
            parentTag = '',liStyle = '',firstTag = '';
            //if ( isWordDocument( html ) ) {
                html = filterPasteWord( html );
            //}
            return html.replace( />[ \t\r\n]*</g, '><' );
        };
不好意思,周末不看csdn
不要这么说,已经很谢谢了! 我这样改的:

case 'width':
if(tag ==='<table'){
 n[i]="width:400px";
}
continue;

//注释掉return function ( html )中的if
            //if ( isWordDocument( html ) ) {
                html = filterPasteWord( html );
            //}

但是表格宽度不变? 我用这个表格测试的(有两处不同的table width):

<table width="1800" bordercolor="#003366" style="width: 1000px;height:300px;">
  <tr>
    <td>11</td>
    <td width="900" height="60">12</td>
  </tr>
  <tr width="1900" height="70">
    <td>21</td>
    <td>22</td>
  </tr>
</table>
审查元素是这样: <table width="1800" bordercolor="#003366" style="font-size:15px;line-height:22px;height:300px"> 没有变成期望的style="width: 800px;"
天雷舞风hehe 2015-02-03
  • 打赏
  • 举报
回复
switch ( name ) {
                            case "mso-padding-alt":
                            case "mso-padding-top-alt":
                            case "mso-padding-right-alt":
                            case "mso-padding-bottom-alt":
                            case "mso-padding-left-alt":
                            case "mso-margin-alt":
                            case "mso-margin-top-alt":
                            case "mso-margin-right-alt":
                            case "mso-margin-bottom-alt":
                            case "mso-margin-left-alt":
                            //ie下会出现挤到一起的情况
//                            case "mso-table-layout-alt":
                            case "mso-height":
                            case "mso-width":
                            case "mso-vertical-align-alt":
                                //trace:1819 ff下会解析出padding在table上
                                if(!/<table/.test(tag))
                                    n[i] = name.replace( /^mso-|-alt$/g, "" ) + ":" + ensureUnits( value );
                                continue;
                            case "horiz-align":
                                n[i] = "text-align:" + value;
                                continue;
 
                            case "vert-align":
                                n[i] = "vertical-align:" + value;
                                continue;
 
                            case "font-color":
                            case "mso-foreground":
                                n[i] = "color:" + value;
                                continue;
 
                            case "mso-background":
                            case "mso-highlight":
                                n[i] = "background:" + value;
                                continue;
 
                            case "mso-default-height":
                                n[i] = "min-height:" + ensureUnits( value );
                                continue;
 
                            case "mso-default-width":
                                n[i] = "min-width:" + ensureUnits( value );
                                continue;
 
                            case "mso-padding-between-alt":
                                n[i] = "border-collapse:separate;border-spacing:" + ensureUnits( value );
                                continue;
 
                            case "text-line-through":
                                if ( (value == "single") || (value == "double") ) {
                                    n[i] = "text-decoration:line-through";
                                }
                                continue;
 
                         
                            //trace:1870
//                            //word里边的字体统一干掉
//                            case 'font-family':
//                                continue;
                            case "mso-zero-height":
                                if ( value == "yes" ) {
                                    n[i] = "display:none";
                                }
                                continue;
                            case 'width':
if(tag ==='<table'){
 n[i]="width:800px";
}
continue;
                            case 'margin':
 
                                if ( !/[1-9]/.test( parts[1] ) ) {
                                    continue;
                                }
                            
                        }
return function ( html ) {
 
            //过了word,才能转p->li
            first = null;
            parentTag = '',liStyle = '',firstTag = '';
            //if ( isWordDocument( html ) ) {
                html = filterPasteWord( html );
            //}
            return html.replace( />[ \t\r\n]*</g, '><' );
        };
不好意思,周末不看csdn
cde32 2015-01-30
  • 打赏
  • 举报
回复
引用 7 楼 xiongsisi1043 的回复:
编辑器代码里,搜索 filterPasteWord函数 在里面的switch(name)里,加入width的逻辑判断 还需要在最后那个return function里边去掉word的判断
太谢谢了,高人啊! 我不太熟悉js,能麻烦您帮我改一下代码好吗? 这是我现在用的旧版代码: filterPasteWord函数的switch(name):

switch ( name ) {
                            case "mso-padding-alt":
                            case "mso-padding-top-alt":
                            case "mso-padding-right-alt":
                            case "mso-padding-bottom-alt":
                            case "mso-padding-left-alt":
                            case "mso-margin-alt":
                            case "mso-margin-top-alt":
                            case "mso-margin-right-alt":
                            case "mso-margin-bottom-alt":
                            case "mso-margin-left-alt":
                            //ie下会出现挤到一起的情况
//                            case "mso-table-layout-alt":
                            case "mso-height":
                            case "mso-width":
                            case "mso-vertical-align-alt":
                                //trace:1819 ff下会解析出padding在table上
                                if(!/<table/.test(tag))
                                    n[i] = name.replace( /^mso-|-alt$/g, "" ) + ":" + ensureUnits( value );
                                continue;
                            case "horiz-align":
                                n[i] = "text-align:" + value;
                                continue;

                            case "vert-align":
                                n[i] = "vertical-align:" + value;
                                continue;

                            case "font-color":
                            case "mso-foreground":
                                n[i] = "color:" + value;
                                continue;

                            case "mso-background":
                            case "mso-highlight":
                                n[i] = "background:" + value;
                                continue;

                            case "mso-default-height":
                                n[i] = "min-height:" + ensureUnits( value );
                                continue;

                            case "mso-default-width":
                                n[i] = "min-width:" + ensureUnits( value );
                                continue;

                            case "mso-padding-between-alt":
                                n[i] = "border-collapse:separate;border-spacing:" + ensureUnits( value );
                                continue;

                            case "text-line-through":
                                if ( (value == "single") || (value == "double") ) {
                                    n[i] = "text-decoration:line-through";
                                }
                                continue;


                            //trace:1870
//                            //word里边的字体统一干掉
//                            case 'font-family':
//                                continue;
                            case "mso-zero-height":
                                if ( value == "yes" ) {
                                    n[i] = "display:none";
                                }
                                continue;
                            case 'margin':

                                if ( !/[1-9]/.test( parts[1] ) ) {
                                    continue;
                                }
                        }
return function ( html ):

return function ( html ) {

            //过了word,才能转p->li
            first = null;
            parentTag = '',liStyle = '',firstTag = '';
            if ( isWordDocument( html ) ) {
                html = filterPasteWord( html );
            }
            return html.replace( />[ \t\r\n]*</g, '><' );
        };
liuxychad 2015-01-29
  • 打赏
  • 举报
回复
可以用几行代码实现,代码少但是很复杂 可以等大神来解决,但你这分数也太少了点
cde32 2015-01-29
  • 打赏
  • 举报
回复
引用 3 楼 liuxychad 的回复:
那可能要先将代码保存到一个容器中,比如一个一行一列的table,然后用js遍历里面的所有元素,如果有超过宽度的元素,就设置它的style,你可以用jquery,很方便 这里刚找到一个修改样式的文章 可以参考一下 http://www.cnblogs.com/chutianshu1981/articles/2935124.html
谢谢! 我用的是百度的富文本编辑器,那里面有这个功能,但是我不太熟悉js,所以找不到具体是哪段代码实现的——没有用jquery。 粘贴时修改宽度样式的代码是不是很复杂?能用几行代码实现吗?
liuxychad 2015-01-29
  • 打赏
  • 举报
回复
算了,我对js也不是特别熟悉
天雷舞风hehe 2015-01-29
  • 打赏
  • 举报
回复
编辑器代码里,搜索 filterPasteWord函数 在里面的switch(name)里,加入width的逻辑判断 还需要在最后那个return function里边去掉word的判断
cde32 2015-01-29
  • 打赏
  • 举报
回复
引用 5 楼 liuxychad 的回复:
可以用几行代码实现,代码少但是很复杂 可以等大神来解决,但你这分数也太少了点
分数不是问题,结帖可以加到一百!不够的话我开几个新帖送分。
liuxychad 2015-01-28
  • 打赏
  • 举报
回复
用js,替换html代码 var reg = /<table[\s]+[^>]+>/gi; // 正则表达式 alert(htmlcontent.replace(reg,'<table width="1800" style="width: 800px;">'));
liuxychad 2015-01-28
  • 打赏
  • 举报
回复
那可能要先将代码保存到一个容器中,比如一个一行一列的table,然后用js遍历里面的所有元素,如果有超过宽度的元素,就设置它的style,你可以用jquery,很方便 这里刚找到一个修改样式的文章 可以参考一下 http://www.cnblogs.com/chutianshu1981/articles/2935124.html
cde32 2015-01-28
  • 打赏
  • 举报
回复
引用 1 楼 liuxychad 的回复:
用js,替换html代码 var reg = /<table[\s]+[^>]+>/gi; // 正则表达式 alert(htmlcontent.replace(reg,'<table width="1800" style="width: 800px;">'));
谢谢!但我是想粘贴到编辑器的时候,把超过编辑器宽度的表格(也许不止一个)宽度转换成编辑器的宽度,这段代码该怎么写?

87,984

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧