web标准常见问题--FF下如何使连续长字段自动换行

ContractedBlock.gifExpandedBlockStart.gifCode
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh" lang="zh">
<head profile="http://www.w3.org/2000/08/w3c-synd/#">
<meta http-equiv="content-language" content="zh-cn" />
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>FF下如何使连续长字段自动换行</title>
<style type="text/css">
*{ margin:0; padding:15px;}
#ff{ width:450px; height:350px; line
-height:25px; overflow:hidden; word-wrap:break-word; border:1px solid #ddd; padding:5px;}
</style> 

</head>
<body>
    
<div id="ff">aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa </div>    
</script>

</body>
</html>

 通常情况下我们可以添加(word-wrap:break-word; display:none;)来实现。在IE浏览器中没有问题,但是firefox不支持换行,我们可以通过js达到换行的效果

None.gif    <script language="javascript">
ExpandedBlockStart.gifContractedBlock.gif    function tobreakword(intLen)
dot.gif{
InBlock.gif        
//获取id=ff的容器
InBlock.gif
        var obj = document.getElementById("ff");
InBlock.gif        
//获取id=ff容器内容的html
InBlock.gif
        var strContent = obj.innerHTML;
InBlock.gif        
//定义一个空字符串
InBlock.gif
        var strTemp = "";
InBlock.gif        
//使用while循环给strContent添加换行
ExpandedSubBlockStart.gifContractedSubBlock.gif
        while(strContent.length > intLen)dot.gif{
InBlock.gif            
//截取0-intLen之间的字符串之后添加换行
InBlock.gif
            strTemp += strContent.substr(0,intLen)+" ";
InBlock.gif            strContent 
= strContent.substr(intLen,strContent.length);            
ExpandedSubBlockEnd.gif            }
        
InBlock.gif        strTemp 
+= " " + strContent;
InBlock.gif        
//把最终的strTemp添加到obj中
InBlock.gif
        obj.innerHTML=strTemp;
ExpandedBlockEnd.gif        }

None.gif        
//判断是否是firfox浏览器
None.gif
        if(document.getElementById && !document.all)  tobreakword(56);
None.gif    
</script>
None.gif

 不过个这js不是很灵活需要根据容器的宽度手动更改intLen的值。

完整代码:

ContractedBlock.gifExpandedBlockStart.gifCode
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh" lang="zh">
<head profile="http://www.w3.org/2000/08/w3c-synd/#">
<meta http-equiv="content-language" content="zh-cn" />
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>FF下如何使连续长字段自动换行</title>
<style type="text/css">
*{ margin:0; padding:15px;}
#ff{ width:450px; height:350px; line
-height:25px; overflow:hidden; word-wrap:break-word; border:1px solid #ddd; padding:5px;}
</style> 

</head>
<body>
    
<div id="ff">aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa </div>
    
<script language="javascript">
    function tobreakword(intLen){
        
//获取id=ff的容器
        var obj = document.getElementById("ff");
        
//获取id=ff容器内容的html
        var strContent = obj.innerHTML;
        
//定义一个空字符串
        var strTemp = "";
        
//使用while循环给strContent添加换行
        while(strContent.length > intLen){
            
//截取0-intLen之间的字符串之后添加换行
            strTemp += strContent.substr(0,intLen)+" ";
            strContent 
= strContent.substr(intLen,strContent.length);            
            }        
        strTemp 
+= " " + strContent;
        
//把最终的strTemp添加到obj中
        obj.innerHTML=strTemp;
        }
        
//判断是否是firfox浏览器
        if(document.getElementById && !document.all)  tobreakword(56);
    
</script>

</body>
</html>

不过有个问题不明白,当我把js添加到head中时,js就不起作用了,哪位高手指教一下。- -

转载于:https://www.cnblogs.com/guopei/archive/2009/08/04/1538549.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值