<!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 id="Head1"><title> </title></head> <body> <h1> JavaScript自定义函数trim过滤空格</h1> 输入:<br /> <textarea name="txt_Input" rows="2" cols="20" id="txt_Input" style="height:100px;width:99%;"> 编程帮手(SlowX)霜叶的编程交流与共享网 </textarea> <br /> <br /> <input type="button" id="btn_OK" value="trim过滤空格" onclick="OkClick()" /> <br /> <br /> 输出:<br /> <textarea name="txt_Output" rows="2" cols="20" id="txt_Output" style="height:100px;width:99%;"></textarea> </body> </html> <script language="javascript" type="text/javascript"> function StrTrim(str) { //删除左右两端的空格 return str.replace(/(^\s*)|(\s*$)/g, ""); } function OkClick() { var inputCtrl = document.getElementById("txt_Input"); var outputCtrl = document.getElementById("txt_Output"); var theValue = inputCtrl.value; theValue = StrTrim(theValue); outputCtrl.value = theValue; } </script>
运行脚本
复制代码
另存代码