日期:2014-05-16 浏览次数:20440 次
?
from:http://www.yaosansi.com/post/304.html
方
法一:
个人认为最好的方法.采用的是正则表达式,这是最核心的原理.
其次.这个方法使用了
JavaScript 的prototype
属性
其
实你不使用这个属性一样可以用函数实现.但这样做后用起来比较方便.
下面就来看看这个属性是怎么来用的.
?
返回对象类型原型的引用。
objectName.prototype
objectName
参数是对象的名称。
说明
用 prototype
属性提供对象的类的一组基本功能。对象的新实例“继承”赋予该对象原型的操作。
例如,要为 Array
对象添加返回数组中最大元素值的方法。要完成这一点,声明该函数,将它加入 Array.prototype,并使用它。
function
array_max( ){
var i, max = this [0];
for (i = 1; i < this .length; i++)
{
if (max < this [i])
max = this
[i];
}
return
max;
}
Array.prototype.max = array_max;
var x = new Array(1, 2, 3, 4, 5, 6);
var
y = x.max( );
?
<SCRIPT LANGUAGE="JavaScript"
>
<!--
//出处:网上搜集
//made by yaosansi 2005-12-02
//For more visit http://www.yaosansi.com
// Trim() , Ltrim() , RTrim()
?
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>
下
面来我们来看看Js脚本中"/s表示什么"
\s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。?
?
请紧记是小写的s