HTML exceeds text multi-line truncation code

Source: Internet
Author: User

The code for HTML multi-line text truncation is as follows:
 

<Div class = "sytm-text-1"> <p> </div> <div class = "sytm-text-2"> <p> </div> CSS:. sytm-text-1 {color: # FFF; background: #000; width: pixel PX; height: 22px ;}. sytm-text-2 {color: # FFF; background: #000; width: pixel PX; height: 44px;} p {line-height: 22px;} JS (introduced jQuery ): $ ("div [class * = 'sytm-text']"). each (function (e) {var divHeight = $ (this ). height (); var $ p = $ ("p", $ (this )). eq (0); while ($ p. outerHeigh T ()> divHeight) {$ p. text ($ p. text (). replace (/(\ s) * ([a-zA-Z0-9] + | \ W )(\. \. \.)? $ /,"..."));};});

Through the above code, we can find that the main principle to achieve this effect is to use the sub-container (p) to compare the height of the parent container (div) to extract Characters Based on the regular expression until the two are equal. Therefore, the key to controlling the number of intercepted rows is the div height.

 

Related Article

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.