首页 > 编程学习 > js 控制英文自动换行 不截断单词的方法

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

发布时间:2022/12/10 16:23:56

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

<!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>


本文链接:https://www.ngui.cc/zz/1568537.html
Copyright © 2010-2022 ngui.cc 版权所有 |关于我们| 联系方式| 豫B2-20100000