Home > Web Front-end > JS Tutorial > JavaScript intercepts a string of specified length and clicks to expand the entire code_javascript skills

JavaScript intercepts a string of specified length and clicks to expand the entire code_javascript skills

WBOY
Release: 2016-05-16 15:27:42
Original
1480 people have browsed it

The length of the article is often not so appropriate. If it is displayed at the original length, it may affect the layout or aesthetics of the web page. At this time, it is necessary to intercept certain strings according to the situation to adapt to the layout. However, when When you click a button, you can still expand all the content. Here is a detailed introduction to how to achieve this effect through an example. The code example is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>字符串截取展开效果</title>
<style type="text/css">
#thediv{
 width:200px;
 margin:0px auto;
}
</style>
<script type="text/javascript"> 
function cutStr(){ 
 var odiv=document.getElementById("thediv"); 
 var str=odiv.innerHTML; 
 var ospan=document.createElement("span"); 
 var olink=document.createElement("a"); 
 ospan.innerHTML=str.substring(0,20); 
 olink.innerHTML=str.length>20&#63;"...":""; 
 olink.href="###"; 
 olink.onclick=function(){ 
  if(olink.innerHTML=="..."){ 
   olink.innerHTML="收起"; 
   ospan.innerHTML=str; 
  }
  else{ 
   olink.innerHTML="..."; 
   ospan.innerHTML=str.substring(0,20); 
  } 
 } 
 odiv.innerHTML=""; 
 odiv.appendChild(ospan); 
 odiv.appendChild(olink); 
}; 
window.onload=function(){
 cutStr();
}
</script> 
<body>
<div id="thediv">脚本之家欢迎您,个人的力量再强也只是一只强壮的而已,如果有分享和胸怀和合作的精神,那么就有可能活得巨大进步。</div> 
</body>
</html>
Copy after login


The above code implements our requirements, intercepts the specified string, and ends with "...". When this ending is clicked, all text content can be expanded. The following is an introduction to the implementation process of this effect.

1. Implementation principle:

Create a span element and a element, then use the substring() function to intercept the specified string and set it as the content of the span element, and then determine whether the length of the original string is greater than the specified length. If it is greater, then a The content in the element is set to "...". Then clear the original div, and then add the span element and a element to the div, thus realizing the interception function. Register a time processing function for the a element. Clicking this button will execute the event processing function. This function first determines what state it is in. If it is in the collapsed state, then the content in the a element will be set to "collapse" , and put all the original characters into span, thus realizing expansion. If it is in the expanded state, then the characters are intercepted and the content in the a element is modified. The principle is roughly the same, you can refer to related readings.

The above content is the JavaScript that this article introduces to you to intercept a string of specified length. Click to expand the entire code. I hope you like it.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template