用js,替换html代码var reg = /<table[\s]+[^>]+>/gi; // 正则表达式alert(htmlcontent.replace(reg,'<table width="1800" style="width: 800px;">'));
解决方案 »
- 询问关于jquery给div的innerHTML赋值问题
- 诡异问题,click事件需要第二次点击才执行
- Ext中gridPanel的enableDragDrop属性设置的问题
- 求一个兼容于ie和firefox的滚轮事件方法
- 字符串的分割
- 求一个javascript函数!!!!
- 我用window.open打开一个窗口里面用confirm失效,为什么?
- 网页一打开就让文本框的值 = 下拉框的值
- 急问!openwindow.document.body is null or not an object!
- 请问怎么监视用户在页面上的所有动作?
- 关于正则匹配所有出现的标签内容问题
- dede织梦系统头部文件无法调用,显示不出来
http://www.cnblogs.com/chutianshu1981/articles/2935124.html
我用的是百度的富文本编辑器,那里面有这个功能,但是我不太熟悉js,所以找不到具体是哪段代码实现的——没有用jquery。
粘贴时修改宽度样式的代码是不是很复杂?能用几行代码实现吗?
在里面的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, '><' );
};
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;"