Intercepts a specified length string length code instance

Source: Internet
Author: User

Intercepts the specified length string length code instance:
The length of the string in the default state is often not able to meet the requirements, such as the length of news list news headlines, if too long tend to cause a line change, affect aesthetics, and then a user experience, so need to intercept the string length according to need, the following share can realize this function on both ends of the code, I hope to help everyone.
The code examples are as follows:
A. css method:
HTML code:

<class= "Cuttext"> ant Tribe welcomes you, only struggle will have a good tomorrow! </ Div >

CSS code:

. Cuttext{width:150px;Height:24px;Overflow:Hidden;White-space:nowrap;Text-overflow:ellipsis;Text-overflow:ellipsis;/*Ie/safari*/-ms-text-overflow:ellipsis;-o-text-overflow:ellipsis;/*Opera*/-moz-binding:URL ("ellipsis.xml#ellipsis");/*FireFox*/}

This method specifies the length, if this length is exceeded, is indicated by an ellipsis.
Two. jquery intercepts the string:

<!DOCTYPE HTML><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><Metaname= "Author"content= "http://www.softwhy.com/" /><title>Intercept string length-ant tribe</title><styletype= "Text/css">ul{width:300px;Height:300px;margin:0px Auto;List-style:None;}ul Li{Height:30px;Line-height:30px;font-size:12px;}</style><Scripttype= "Text/javascript"src= "Mytest/jquery/jquery-1.8.3.js"></Script><Scripttype= "Text/javascript">$.fn.substr=function(length,content) {$ ( This). each (function(i,item) {varVal=$ (item). HTML (); if(!val)return; if(Val.length>length) {Val=val.substring (0, length); Val+=content||"...";     $ (item). HTML (val); }   })} functionSubtdcontent () {$ ('Li'). substr ( -); } $ (document). Ready (function() {subtdcontent ();})</Script> </Head> <Body> <ul>  <Li>Ant Tribe welcomes you, only struggle will have a better tomorrow</Li>  <Li>Every day is a new one, so take a good hold of it.</Li>  <Li>Never think of tomorrow, for the present is the most real.</Li>  <Li>No one is a master in the beginning, all from the rookie grew up</Li></ul></Body> </HTML> 

The above code can also achieve the interception of string length effect, how to implement this is not introduced here, if there are any questions can be posted message.
Related reading:
The 1.each () function can refer to the each () method section of jquery .
The 2.substring () function can refer to the substring () method section of the JavaScript string object .

The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=8831

For more information, refer to: http://www.softwhy.com/jquery/

Intercepts a specified length string length code instance

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.