The length of the article is often not so right, if the original length of the show, then it may affect the layout of the Web page or aesthetic, this time will be based on the right amount of interception of a certain string to fit the layout, but when clicked on a button can still expand all the content, Here is an example of how to implement this effect in detail, with the following code example:
The above code implements our request, intercepts the specified string, and then ends with "...", and when clicked at the end, can expand all the text content, the following is the implementation of this effect.
I. Principle of realization:
Create a SPAN element and a element, and then use the substring () function to intercept the specified string and set it to the contents of the span element, and then determine whether the original string is longer than the specified length, or if it is greater than, set the content in the A element to "...". The original Div is then emptied, and the span element and a element are added to the Div, which enables the interception function. To register a time handler for a element, click this button to perform the event handler function, this function first determines what state it is in, and if it is in a closed state, the content in the a element is set to "close" and the original character is placed in span, so that the expansion is done, and if out of the expanded state, Then it is to intercept the characters and modify the contents of the A element. The principle is generally the same, you can refer to related reading.
The above content is this article to introduce the JavaScript interception specified length string Click May expand all code, hoped everybody likes.