- Prototype 教程
- Prototype - 首页
- Prototype - 简要概述
- Prototype - 有用的特性
- Prototype - 实用工具方法
- Prototype - Element 对象
- Prototype - 数字处理
- Prototype - 字符串处理
- Prototype - 数组处理
- Prototype - 哈希处理
- Prototype - 基本对象
- Prototype - 模板
- Prototype - 枚举
- Prototype - 事件处理
- Prototype - 表单管理
- Prototype - JSON 支持
- Prototype - AJAX 支持
- Prototype - Range 表达方式
- Prototype - 间隔执行
- Prototype 有用的资源
- Prototype - 快速指南
- Prototype - 有用的资源
- Prototype - 讨论
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
广告