为javascript的String对象增加一个format(args[])方法。
此方法可以将字符串中的{0},{1},{2}等占位符替换为参数传入的值。
如: var msg = 'The value of {0} must be less than {1}.'.format(['score', '100']);
msg的值为 The value of score must be less than 100.
有可能出现{{0}}的情况,替换结果应该为{str},请注意
此方法可以将字符串中的{0},{1},{2}等占位符替换为参数传入的值。
如: var msg = 'The value of {0} must be less than {1}.'.format(['score', '100']);
msg的值为 The value of score must be less than 100.
有可能出现{{0}}的情况,替换结果应该为{str},请注意
解决方案 »
- 能不能通过id找到一个hidden在table的哪一行
- javascript日期参数的问题,如何连接为一个带参数的URL?
- 使用radio的一个疑惑
- 能否将PHP变量传给JS的变量啊?
- 求教一个Select连动的问题,在线等待!
- javaScript学习中的一个疑问,大家看看先啊
- 怎样屏蔽掉IE“查看”中的“源文件”选项呀或使它不可用!
- 大家好 新浪网页浏览时会出现iCast播放这是什么软件做的到那里下载 谢谢
- fireEvent()与attachEvent()的区别. 外加cancelBubble
- 请教高手关于连续滚动的问题
- 谷歌浏览器如何获得<input tupe="file"... 的完整路径███████████████████████████████████████████
- 如何用JS得到当前系统日期是星期几
前段时间刚好讨论过
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
String.prototype.format = function(argu){
var val = this;
if(argu instanceof Array){
for(var i=0; i<argu.length; i++){
val = val.replace("{" + i + "}", argu[i]);
}
}
return val;
};
var msg = "The value of {0} must be less than {1}.".format(['score', 100]);alert(msg);
</script>
</head>
<body>
</body>
</html>