JS text Automatic "newline" understanding

Source: Internet
Author: User
Tags html tags

Definition: Text wraps automatically when the length of the text is greater than the width of the container.

There will be no problems with Chinese, as well as General English words.

The general problem is a long string of English words or strings that are not separated, there are several situations:

1, the general situation (no floating; No display:inline-block;

This situation is best handled, just add word-wrap:break-word;

2, the text outside the container has floating, or have display:inline-block;

For non-FF browsers, Word-wrap:break-word; Word-break:break-all; can;

For FF browsers, because the Word-break:break-all is not recognized, the method is invalid, two ways:

Law 1: Look at the floating or display:inline-block, whether you must, if necessary, remove it; If you must, then simply rely on CSS tutorial can not achieve this goal, to turn to JS Bar.

Method 2:js, but there are some limitations to this method, only applicable to the case of plain text. There is a problem if you want to include other HTML tags in the text segment. For example, if you include a label <span></span>, so that the text in the label can be decorated, and if the break is just inside the label, the label will be corrupted and become a meaningless character.

Refer to the JS code as follows (refer to the online code):

<script>
function Tobreakword (Intlen) {
var Obj=document.getelementbyid ("FF");
var strcontent=obj.innerhtml;
var strtemp= "";
while (Strcontent.length>intlen) {
Strtemp+=strcontent.substr (0,intlen) + "& #10;";
Strcontent=strcontent.substr (intlen,strcontent.length);
}
strtemp+= "& #10;" +strcontent;
Obj.innerhtml=strtemp;
}
if (document.getElementById &&!document.all)
Tobreakword (37)
</script>

3, table layout (FF situation)

For a table layout, there is a benefit at this point, as long as the Word-wrap:break-word;overflow:hidden is set on a column/row, and then the table is table-layout:fixed; To achieve the automatic line wrapping effect.

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.