JS implementation beyond a certain number of words hidden and with the ellipsis "..." instead, click and can be expanded and closed,

Source: Internet
Author: User

The original simple explanation: put two identical Div, put the text you want to display in. When the page is initialized, the first Div shows, the second one

Div Hidden, that's that simple. (PS: You can copy the code directly into your own project to see the effect)

Style section (remember to quote jquery if you like handwriting native JS when I didn't say) **********

<style>. Ms-cont2{Display:None;}. Ms-cont2,.ms-cont1{cursor:Pointer;}</style>

Body part **********

<class= "Ms-cont1">{!! $v [' content ']!!} </ Div > <  class= "Ms-cont2">{!!! $v [' content ']!!} </ Div >

JS section ************

<Script> functioncutstring (str, len) {//the Length property reads 1 Chinese characters . if(Str.length*2 <=Len) {  returnstr;} varstrlen= 0; vars= "";  for(varI= 0; I<str.length; I++) {s=s+Str.charat (i); if(Str.charcodeat (i)>  -) {strlen=strlen+ 2; if(strlen>=Len) {    returns.substring (0, S.length-1) + "..."; }  } Else{strlen=strlen+ 1; if(strlen>=Len) {    returns.substring (0, S.length-2) + "..."; }  } } returns;}//the above cutstring method for reference: http://www.jb51.net/article/91716.htm, thank you very much! $ (document). Ready (function(){varCont1= $('. Ms-cont1'), Cont2= $('. Ms-cont2'); Cont1.click (function(){$( This). Hide (). Next (). Show ();}); Cont2.click (function(){$( This). Hide (). Prev (). Show ();}) Cont1.each (function(){var that= $( This), txt=That.text ();//console.log (TXT);that.text (cutstring (TXT, -));})})</Script>

Front page Show **********

Post-load effects

Post-click Effects

After clicking again

JS implementation beyond a certain number of words hidden and with the ellipsis "..." instead, click and can be expanded and closed,

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.