此方法将字符串截断为给定长度并为其附加后缀(表示它只是摘录)。
如果未指定,长度参数默认为 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>作者:terry,如若转载,请注明出处:https://www.web176.com/prototype_api/8813.html
支付宝
微信