2008 2008年 9月 1日
從JavaScript數組傳遞的字符串相匹配的特定項目
這裡的快速擴大本地的JavaScript數組對象,只是為了做到這一點的方式...
Array.prototype. removeItem =function(str) {
for(i=0; i<this.length ; i++){
if(escape(this[i]).match(escape(str.trim()))){
this.splice(i, 1); break;
}
}
return this;
}
所以,現在你可以做這樣的事情......
var animals= new Array("dog","lion","cat","tiger","elephant");
animals. removeItem ('tiger');現在的動物數組將包含“狗”,“獅子”,“貓”,“大象”;
注:這裡是字符串修剪原型...
String.prototype.trim=function(str) {
str = this != window? this : str;
return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
享受....










































