Processing method for multi-line text truncation display based on Javascript
Technical Field
The invention particularly relates to a processing method for multi-line text truncation display based on Javascript, and belongs to the technical field of computer information.
Background
With the continuous development of computer network technology, BS architecture systems are widely used in various industries, and therefore, the loading content of web programs is more and more diversified. Nowadays, text contents in web pages are more and more abundant, different text contents need to be displayed in the same display area of the web pages, and for multi-line texts, a browser does not have a mode of being directly used for intercepting display.
Disclosure of Invention
The invention aims to provide a processing method for multi-line text truncation display based on Javascript.
In order to achieve the purpose, the invention is realized by the following technical scheme:
a processing method for multi-line text truncation display based on Javascript comprises the following steps:
acquiring all contents in a display area; adding a specific empty element behind each character, and calculating the position of each character through the added elements; and intercepting the characters which exceed the display area, updating the characters to the display area, and finishing multi-line intercepting display.
The preferable scheme of the processing method for intercepting and displaying the multi-line text based on the Javascript comprises the following steps: step S01: the position attribute of the DIV container is set as: absolute or relative;
step S02: obtaining the width and height dimensions of the DIV container;
step S03: acquiring all text contents in a DIV container;
step S04: dividing the text content acquired in the step S03 into single character arrays;
step S05: traversing the array, adding a null element < span > behind each character, recording the index value of the corresponding array element into the attribute of the < span > element, and splicing into a new character string;
step S06: splicing and omitting the representation text before the spliced character strings;
step S07: updating the generated text character string into a DIV container;
step S08: traversing the empty < span > elements added in the step s05, obtaining the position of each element, and comparing with the width and height sizes obtained in the step s02
Step S09: calculating a < span > element exceeding the DIV container firstly, and acquiring an index value stored in the element;
step S10: dividing the array obtained in s04 according to the index value obtained in s09, splicing the array into text strings, and adding a set text with omitted representation;
step S11: and updating the text in the S10 into a DIV container to complete the multi-line text truncation display.
The invention has the advantages that: according to the width and height of a text container in the webpage, multi-line text truncation display can be realized.
Detailed Description
The technical solutions in the embodiments of the present invention are clearly and completely described below, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
As mentioned in the background art, the following problems exist in the prior art: for multi-line text, the browser does not have a direct way to truncate the display.
Aiming at the problems, the invention adopts the following technical scheme: the processing method for multi-line text truncation display based on Javascript comprises the following steps: acquiring all contents in a display area; adding a specific empty element behind each character, and calculating the position of each character through the added elements; and intercepting the characters which exceed the display area, updating the characters to the display area, and finishing multi-line intercepting display.
DETAILED DESCRIPTION OF EMBODIMENT (S) OF INVENTION
The processing method for multi-line text truncation display based on Javascript comprises the following steps:
step S01: the position attribute of the DIV container is set as: absolute or relative;
step S02: obtaining the width and height dimensions of the DIV container;
step S03: acquiring all text contents in a DIV container;
step S04: dividing the text content acquired in the step S03 into single character arrays;
step S05: traversing the array, adding a null element < span > behind each character, recording the index value of the corresponding array element into the attribute of the < span > element, and splicing into a new character string;
step S06: splicing and omitting the representation text before the spliced character strings;
step S07: updating the generated text character string into a DIV container;
step S08: traversing the empty < span > elements added in the step s05, obtaining the position of each element, and comparing with the width and height sizes obtained in the step s02
Step S09: calculating a < span > element exceeding the DIV container firstly, and acquiring an index value stored in the element;
step S10: dividing the arrays obtained in s04 according to the index values obtained in s09, splicing the arrays into text strings, and adding the set text with omission representation;
step S11: and updating the text in the S10 into the DIV container to complete the multi-line text truncation display.
Finally, it should be noted that: although the present invention has been described in detail with reference to the foregoing embodiments, it will be apparent to those skilled in the art that changes may be made in the embodiments and/or equivalents thereof without departing from the spirit and scope of the invention. Any modification, equivalent replacement, or improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.