Javascript: 截取字串多出來並用省略符號[...]顯示

來源:互聯網
上載者:User

標籤:

/背景知識/

substring 方法用於提取字串中介於兩個指定下標之間的字元

substring(start,end)

開始和結束的位置,從零開始的索引

 

參數描述

start 必需。一個非負的整數,規定要提取的子串的第一個字元在 stringObject 中的位置。
stop 可選。一個非負的整數,比要提取的子串的最後一個字元在 stringObject 中的位置多 1。如果省略該參數,那麼返回的子串會一直到字串的結尾。

 

 

傳回值

一個新的字串,該字串值包含 stringObject 的一個子字串,其內容是從 start 處到 stop-1 處的所有字元,其長度為 stop 減 start。

 

說明

substring 方法返回的子串包括 start 處的字元,但不包括 end 處的字元。


如果 start 與 end 相等,那麼該方法返回的就是一個空串(即長度為 0 的字串)。


如果 start 比 end 大,那麼該方法在提取子串之前會先交換這兩個參數。

 

如果 start 或 end 為負數,那麼它將被替換為 0

 

 

/實際應用:截取字串多出來並用省略符號[...]顯示/

 

線上示範:http://codepen.io/anon/pen/yNXyMX

 

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>js 截取字串多出來並用省略符號...顯示</title><style type="text/css">.content{width: 200px;        margin: 100px auto;padding: 15px;background-color: orange;color: #fff;}.btn {  float: right;  background-color: #fff;  color: #333;  font-size: 12px;  padding: 4px 8px;  border-radius: 3px;  margin-top: 5px;  text-shadow: 0 0 1px rgba(0,0,0,.25);  box-shadow: 0 0 1px rgba(0,0,0,.25);     }.move{ -webkit-transition:all ease-in-out .35s; transition:all ease-in-out .35s; -webkit-animation:showIn 1.4s cubic-bezier(0, 0, 0.2, 1);         animation:showIn 1.4s cubic-bezier(0, 0, 0.2, 1);         -webkit-animation-fill-mode: backwards;         -moz-animation-fill-mode: backwards;         -o-animation-fill-mode: backwards;          animation-fill-mode: backwards;     }@keyframes showIn{    0%{ opacity: .5;}    60%{transform: scale(1.2);}    100% { -o-transform: scale(1.0);opacity: 1.0;}    }@-webkit-keyframes showIn{    0%{ opacity: .5;height: 20%;}    60%{-webkit-transform: scale(1.2);}    100% { -webkit-transform: scale(1.0);opacity: 1.0;}   }</style></head><body><div class="content"> <span>substring 方法用於提取字串中介於兩個指定下標之間的字元substring(start,end)開始和結束的位置,從零開始的索引substring 方法返回的子串包括 start 處的字元,但不包括 end 處的字元。如果 start 與 end 相等,那麼該方法返回的就是一個空串(即長度為 0 的字串)。如果 start 比 end 大,那麼該方法在提取子串之前會先交換這兩個參數。如果 start 或 end 為負數,那麼它將被替換為 0</span><span class="btn">收縮</span></div><script type="text/javascript">var oDiv=document.getElementsByTagName(‘div‘)[0];var arr_span=document.getElementsByTagName(‘span‘);        var str=arr_span[0].innerHTML;var onOff=true;       arr_span[1].onclick=function(){    if(onOff){        arr_span[0].innerHTML=str.substring(0,43)+‘...‘;    arr_span[1].innerHTML=‘展開‘;    oDiv.classList.add(‘move‘);    onOff=false;    }else{    arr_span[0].innerHTML=str;    arr_span[1].innerHTML=‘收縮‘;    oDiv.classList.remove(‘move‘);    onOff=true;    }      }</script></body></html>

  

 

Javascript: 截取字串多出來並用省略符號[...]顯示

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.