js 控制英文自动换行 不截断单词的方法

自己感觉文章逻辑很清晰 像对中文字符的判断 很不错!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<script type="text/javascript">
//函数功能:在指定长度处自动添加换行符,以英文长度为准,及8代表8个英文或4个汉字
function fnAddBr(id, iPerLineLen){
var sStr=document.getElementById(id).innerHTML;
alert(sStr.length);
if(sStr.replace(/[^/x00-/xff]/g,"xx").length <= iPerLineLen)
{
return -1;
}
var str="";
var l=0;
var schar;
for(var i=0;schar=sStr.charAt(i);i++){
str+=schar;
l+=(schar.match(/[^/x00-/xff]/)!=null?2:1);
if(l>= iPerLineLen){
//判断是不是空格
if(schar==" ")
{
str+="/n";
l=0;
}

}
}
document.getElementById(id).innerHTML=str;
}
</script>
<div id="content" style="border:1px solid #ccc">
The value of China's currency, the yuan, hit a record high for the fifth straight trading day Monday with the central parity rate at 6.5705 yuan per U.S. dollar, according to the China Foreign Exchange Trading system.
The central parity rate of the yuan, or RMB, was 76 basis points up from 6.5781 on Feb. 18, the previous trading day.
China's central bank announced on June 19 last year that it would reform the yuan exchange rate formation mechanism to improve its flexibility.
On China's foreign exchange spot market, the yuan can rise or fall 0.5 percent from the central parity rate each trading day.
The central parity rate of the RMB against the U.S. dollar is based on a weighted average of prices before the opening of the market each business day.
</div>
<div id="content1" style="border:1px solid #ccc">
The value of China's currency, the yuan, hit a record high for the fifth straight trading day Monday with the central parity rate at 6.5705 yuan per U.S. dollar, according to the China Foreign Exchange Trading system.
The central parity rate of the yuan, or RMB, was 76 basis points up from 6.5781 on Feb. 18, the previous trading day.
China's central bank announced on June 19 last year that it would reform the yuan exchange rate formation mechanism to improve its flexibility.
On China's foreign exchange spot market, the yuan can rise or fall 0.5 percent from the central parity rate each trading day.
The central parity rate of the RMB against the U.S. dollar is based on a weighted average of prices before the opening of the market each business day.
</div>
<script type="text/javascript">
fnAddBr("content",100);
fnAddBr("content1",50);
</script>
</form>
</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值