易截截图软件、单文件、免安装、纯绿色、仅160KB

增加 javascript 的 trim 函数


去除字符串左右两端的空格,在vbscript里面可以轻松地使用 trim、ltrim 或 rtrim,但在js中却没有这3个内置方法,需要手工编写。下面的实现方法是用到了正则表达式,效率不错,并把这三个方法加入String对象的内置方法中去。
<input type="text" name="mytxt" value="   12345678    " /><br>
<input type="button" name="cmd1" onclick="mytxt2.value=mytxt.value.trim()" value="去两边的空格"/>
<input type="text" name="mytxt2"/><br>
<input type="button" name="cmd1" onclick="mytxt3.value=mytxt.value.ltrim()" value="去左边的空格"/>
<input type="text" name="mytxt3"/><br>
<input type="button" name="cmd1" onclick="mytxt4.value=mytxt.value.rtrim()" value="去右边的空格"/>
<input type="text" name="mytxt4"/><br>
<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>
写成函数可以这样:
<script type="text/javascript">
function trim(str){  //删除左右两端的空格
 return str.replace(/(^s*)|(s*$)/g, "");
}
function ltrim(str){  //删除左边的空格
 return str.replace(/(^s*)/g,"");
}
function rtrim(str){  //删除右边的空格
 return str.replace(/(s*$)/g,"");
}
</script>


相关文档:

[翻译]High Performance JavaScript(002)

Grouping Scripts 成组脚本
    Since each <script> tag blocks the page from rendering during initial download, it's helpful to limit the total number of <script> tags contained in the page. This applies to both inline scripts as well as those in external files. Every time ......

JavaScript常用对象详解

SCRIPT 标记  
用于包含JavaScript代码.  
属性  
LANGUAGE 定义脚本语言  
SRC 定义一个URL用以指定以.JS结尾的文件  
window对象  
每个HTML文档的顶层对象.  
属性  
frames[] 子桢数组.每个子桢数组按源文档中定义的 ......

通过javascript获得url参数

页面提交数据一般有两种方法:get,post。post就是所谓的form提交,使用视图;get是通过url提交。
Get方法一般用后台代码(如asp,asp.net)获得参数,代码很简单:Request.QueryString["id"];即可获取。 
有些时候需要直接在前台获取url参数,要用到javascript,js没有直接获取url参数的方法,那么,我们如何通过js ......

公历转换农历的算法(JavaScript)

很经典的一个算法,学习…ing… :
<!--  中国农历开始  -->
<SCRIPT language=JavaScript>
<!--
var lunarInfo=new Array(
0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2,
0x04ae0,0x0a5b6,0x0a4d0,0x0d25 ......

JavaScript 备忘录

 
function
getVerificationCode()//取得验证码
{
  
    
var
para =
new
Array(
"1"
,
"2"
,
"3"
,
"4"
,
"5"
,
"6"
,
"7"
,
"8"
,
"9"
,
 &nbs ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号