<div class="htmlarea"> <textarea id="runcode46749"> <input type="text" name="mytxt" value=" 12345678 "> <input type="button" name="cmd1" onclick="mytxt2.value=mytxt.value.trim()" value="去两边的空格"> <input type="text" name="mytxt2"> <input type="button" name="cmd1" onclick="mytxt3.value=mytxt.value.ltrim()" value="去左边的空格"> <input type="text" name="mytxt3"> <input type="button" name="cmd1" onclick="mytxt4.value=mytxt.value.rtrim()" value="去右边的空格"> <input type="text" name="mytxt4"> <script language="javascript"> 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,""); } </script> </textarea><br><input onclick="runEx('runcode46749')" type="button" value="运行代码"> <input onclick="doCopy('runcode46749')" type="button" value="复制代码"> <input onclick="doSave(runcode46749)" type="button" value="保存代码">[Ctrl+A 全选 注:<a href="http://www.jb51.net/article/23421.htm" title="查看具体详情" target="_blank">如需引入外部Js需刷新才能执行</a>]</div> <br>写成函数可以这样: <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="38463" class="copybut" id="copybut38463" onclick="doCopy('code38463')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code38463"> <br><script type="text/javascript"> <BR>function trim(str){ //删除左右两端的空格 <BR>return str.replace(/(^\s*)|(\s*$)/g, ""); <BR>} <BR>function ltrim(str){ //删除左边的空格 <BR>return str.replace(/(^\s*)/g,""); <BR>} <BR>function rtrim(str){ //删除右边的空格 <BR>return str.replace(/(\s*$)/g,""); <BR>} <BR></script><br> </div>