Prototype - truncate() 方法



此方法把字串截斷為給定長度,並附加一個字尾(表示它只是一個摘錄)。

如果不指定,length 引數預設為 30,字尾為 "..."。

語法

string.truncate([length = 30[, suffix = '...']]) ;

返回值

返回一個截斷字串。

示例

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showResult() {
            var str = 'A random sentence whose length exceeds 30 characters.';
            alert("str.truncate() : " + str.truncate() );
            alert("str.truncate( 10 ) : " + str.truncate(10) );
            alert("str.truncate( 10, '...') : " + 
            str.truncate(10, '...') );
         }
      </script>
   </head>

   <body>
      <p>Click the button to see the result.</p>
      <br />
      <br />
      <input type = "button" value = "Result" onclick = "showResult();"/>
   </body>
</html>

輸出

prototype_string_processing.htm
廣告