哪为兄台知道JavaScript去掉字符串左右空格的函数怎样写?

dimaomao 2002-07-11 10:45:59
需要一个JavaScript函数象VBScript的Trim功能一样,能去掉字符串两边的空格?
...全文
34 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Naola2001 2002-07-11
  • 打赏
  • 举报
回复
有收获
meizz 2002-07-11
  • 打赏
  • 举报
回复
<input type=text name=txt size=10><br>
<input type=button value=check onclick="check1()">

<script language="Javascript"><!--
String.prototype.Trim = function(){return this.replace(/(^\s*)|(\s*$)/g, "");}
function check1(){
var tt = document.all.txt.value;
var s;
if (tt.Trim().length==0){
alert("输入不能为空!");
document.all.txt.value = "";
return;}
tt = tt.Trim();
for (i = 0; i < tt.length; i++){
s = tt.charCodeAt(i);
if ((s==13) || (s==9)){
alert("你输入的字符中含有tab或者回车键!");
document.all.txt.value = tt;
return; }
}
return;
}
// --></script>
meizz 2002-07-11
  • 打赏
  • 举报
回复
//Javascript中为String对象添加trim
String.prototype.Trim = function(){return this.replace(/(^\s*)|(\s*$)/g, "");}
String.prototype.LTrim = function(){return this.replace(/(^\s*)/g, "");}
String.prototype.Rtrim = function(){return this.replace(/(\s*$)/g, "");}
dimaomao 2002-07-11
  • 打赏
  • 举报
回复
对不起,还有如何判断一个文本框输入的是空个或Tab以及回车等?

87,994

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧