JavaScript a custom-length text-wrapping function _javascript Tips

Source: Internet
Author: User
Many friends in the process of making Web pages always found that some time some English will put the original good form to distraction and lead to deformation, although you can use style= "Table-layout:fixed;word-wrap:break-word;word-break: Break-all "To solve this problem, but sometimes it will cause the content display is not complete and is hidden, and did not do the effect of automatic wrapping.

So I want to write a function to fix this flaw, this function is very simple, but here only provides a way to solve this problem.

function FNADDBR (ID, Iperlinelen) {...}
ID is the length of each line for the Id,iperlinelen of the text block to be modified
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en" "HTTP://WWW.W3.ORG/TR/HTML4/STRICT.DTD" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>untitled document</title>
<body>
<script type= "Text/javascript" >
Function: Automatically add line breaks at specified lengths, whichever is the length of the English language, and 8 for 8 English or 4 Chinese characters
function FNADDBR (ID, Iperlinelen) {
var Sstr=document.getelementbyid (id). InnerHTML;
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) {
str+= "<br/>\n";
l=0;
}
}
document.getElementById (ID). innerhtml=str;
}
</script>
<div id= "Content" style= "border:1px solid #ccc" >
I do the Web page when the content added to the Web page is always horizontal to support the table is very wide, it is difficult to see;
I'm on the plus.
Style= "Table-layout:fixed;word-wrap:break-word;word-break:break-all"

After this code, the table will not be wide, but the content is not complete, just show the same width of the table content, the other is blocked or filtered out.
My character is Chinese, does this matter?

Is there any way to make the text in the table to the number of words to automatically change the line? Or can change lines and do not support large forms, and do not omit content!

My content is called in the dynamic of this tag!
</div>

<div id= "Content1" style= "border:1px solid #ccc" >
I do the Web page when the content added to the Web page is always horizontal to support the table is very wide, it is difficult to see;
I'm on the plus.
Style= "Table-layout:fixed;word-wrap:break-word;word-break:break-all"

After this code, the table will not be wide, but the content is not complete, just show the same width of the table content, the other is blocked or filtered out.
My character is Chinese, does this matter?

Is there any way to make the text in the table to the number of words to automatically change the line? Or can change lines and do not support large forms, and do not omit content!

My content is called in the dynamic of this tag!
</div>

<script type= "Text/javascript" >
FNADDBR ("Content", 25);
FNADDBR ("Content1", 50);
</script>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.