51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 4493|回复: 1
打印 上一主题 下一主题

[原创] 网页设计自动换行和去除HTML标签总结

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2012-11-20 14:29:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
CSS自动换行问题
1.        你定死表格的宽度,即给表格一个宽度值(是数值,不是百分比)   
2.        强制不换行
div{
//white-space:不换行;normal 默认;nowrap强制在同一行内显示所有文本,直到文本结束或者遭遇 br 对象
white-space:nowrap; }
自动换行
div{
Word-wrap: break-word;
//word-break设置强行换行;normal 亚洲语言和非亚洲语言的文本规则,允许在字内换行
word-break: normal;
}
强制英文单词断行
div{
word-break:break-all;
}
3.        总结了一下,只要在CSS中定义了如下句子,可保网页不会再被撑开了。
table{table-layout: fixed;}
td(word-break: break-all; word-wrap:break-word;)
注释一下:
        第一条table{table-layout: fixed;},此样式可以让表格中有!!!(感叹号)之类的字符时自动换行。
        td{word-break: break-all},一般用这句这OK了,但在有些特殊情况下还是会撑开,因此需要再加上后面一句(word-wrap:break-word;)就可以解决。此样式可以让表格中的一些连续的英文单词自动换行。
(1)语法:
word-break : normal | break-all | keep-all
参数:
normal :  依照亚洲语言和非亚洲语言的文本规则,允许在字内换行
break-all :  该行为与亚洲语言的normal相同。也允许非亚洲语言文本行的任意字内断开。该值适合包含一些非亚洲文本的亚洲文本
keep-all :  与所有非亚洲语言的normal相同。对于中文,韩文,日文,不允许字断开。适合包含少量亚洲文本的非亚洲文本
(2)语法:
word-wrap : normal | break-word
参数:
normal :  允许内容顶开指定的容器边界
break-word :  内容将在边界内换行。如果需要,词内换行(word-break)也行发生
说明:
设置或检索当当前行超过指定容器的边界时是否断开转行。
对应的脚本特性为word-wrap。请参阅我编写的其他书目。
(3)语法:
table-layout : auto | fixed
参数:
auto :  默认的自动算法。布局将基于各单元格的内容。表格在每一单元格读取计算之后才会显示出来。速度很慢
fixed :  固定布局的算法。在这算法中,水平布局是仅仅基于表格的宽度,表格边框的宽度,单元格间距,列的宽度,而和表格内容无关
说明:
如何让表格自动换行?
设置或检索表格的布局算法。
对应的脚本特性为tableLayout。
4.        按照网上大多数文章的说法,只要在CSS中加入:
代码
..........
Code:
table {<br />
table-layout:fixed;word-break:break-all;word-wrap:break-word;}<br />
div{word-break:break-all;word-wrap:break-word;}
就可以解决表格和层被撑破,最初我也是这样做的。不过这样的代码会造成一个问题,你会发现英文词全部被截断了,这不符合英语的书写习惯也不利于阅读。
后来我发现上述代码改写一下就可以做到既防止表格/层撑破又防止单词断裂了。
如下:
代码
Code:
table {
table-layout: fixed;
word-wrap:break-word;
}
div {
word-wrap:break-word;
}

javascript去除HTML标签
//str 传入的html源文件 noEnter 表示是否去除换行
// 不全的话 可依据格式 自己添加.
function html2txt(str,noEnter){
var html = str;
html = html.replace(/<!--[\s\S]*?-->/img, "");//注释
html = html.replace(/<[\/]*table[^>]*>/img, "\n");//table
html = html.replace(/<[\/]*tbody[^>]*>/img, "");//tbody
html = html.replace(/<[\/]*tr[^>]*>/img, "\n");//tr
html = html.replace(/<[\/]*td[^>]*>/img, "\n");//td
html = html.replace(/<[\/]*p[^>]*>/img, "\n");//p
html = html.replace(/<[\/]*a[^>]*>/img, "\n");//a
html = html.replace(/<[\/]*col[^>]*>/img, "\n");//col
html = html.replace(/<[\/]*br[^>]*>/img, "\n");//br
html = html.replace(/<[\/]*[^>]*>/img, "\n");//
html = html.replace(/<[\/]*span[^>]*>/img, "");//span
html = html.replace(/<[\/]*center[^>]*>/img, "");//center
html = html.replace(/<[\/]*ul[^>]*>/img, "");//ul
html = html.replace(/<[\/]*i[^>]*>/img, "");//i
html = html.replace(/<[\/]*li[^>]*>/img, "");//li
html = html.replace(/<[\/]*b[^>]*>/img, "");//b
html = html.replace(/<[\/]*hr[^>]*>/img, "");//hr
html = html.replace(/<[\/]*h\d+[^>]*>/img, "");//h1,2,3,4,5,6
html = html.replace(/<STYLE[\s\S]*?<\/STYLE>/img, "");//样式
html = html.replace(/<script[\s\S]*?<\/script>/img, "");//引用的脚本
//html = html.replace(/<[\?!A-Za-z\][^><]*>/img, "");alert("str:"+html)
html = html.replace(/\r/img, "");//换行
html = html.replace(/\n/img, "\r\n");//回车
html = html.replace(/[ |\s]*\r\n[ |\s]*\r\n/img, "\r\n");
//html = reg.replace(html,@"(\r\n)[^  ]/img,"$1");
html = formatHtml(html);
if(noEnter){
   html = html.replace(/\r\n/img, "");
   html = html.replace(/\n/img, "");
   html = html.replace(/\r/img, "");
}
return (html);
}
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
发表于 2013-1-25 18:16:50 | 只看该作者
Thank you very much for sharing!The good man!The good life of peace!
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-4-20 10:32 , Processed in 0.070577 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表