此方法将字符串截断为给定长度并为其附加后缀(表示它只是摘录)。
如果未指定,长度参数默认为 30,后缀为“…”。
语法
string.truncate([length = 30[, suffix = '...']]) ;
返回值
返回截断的字符串。
例子
HTML
x
23
23
1
<html>
2
<head>
3
<title>Prototype examples</title>
4
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
5
6
<script>
7
function showResult() {
8
var str = 'A random sentence whose length exceeds 30 characters.';
9
alert("str.truncate() : " + str.truncate() );
10
alert("str.truncate( 10 ) : " + str.truncate(10) );
11
alert("str.truncate( 10, '...') : " +
12
str.truncate(10, '...') );
13
}
14
</script>
15
</head>
16
17
<body>
18
<p>Click the button to see the result.</p>
19
<br />
20
<br />
21
<input type = "button" value = "Result" onclick = "showResult();"/>
22
</body>
23
</html>
阅读剩余 76%
作者:terry,如若转载,请注明出处:https://www.web176.com/prototype_api/8813.html