用js,替换html代码var reg = /<table[\s]+[^>]+>/gi;  // 正则表达式alert(htmlcontent.replace(reg,'<table width="1800" style="width: 800px;">'));

解决方案 »

  1.   

    那可能要先将代码保存到一个容器中,比如一个一行一列的table,然后用js遍历里面的所有元素,如果有超过宽度的元素,就设置它的style,你可以用jquery,很方便这里刚找到一个修改样式的文章 可以参考一下
    http://www.cnblogs.com/chutianshu1981/articles/2935124.html
      

  2.   

    谢谢!
    我用的是百度的富文本编辑器,那里面有这个功能,但是我不太熟悉js,所以找不到具体是哪段代码实现的——没有用jquery。
    粘贴时修改宽度样式的代码是不是很复杂?能用几行代码实现吗?
      

  3.   

    编辑器代码里,搜索 filterPasteWord函数
    在里面的switch(name)里,加入width的逻辑判断还需要在最后那个return function里边去掉word的判断
      

  4.   

    太谢谢了,高人啊!
    我不太熟悉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, '><' );
            };
      

  5.   

    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
      

  6.   

    不要这么说,已经很谢谢了!
    我这样改的: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;"