js 为数组编写该方法;indexOf
<!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> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <script> /*var str = 'aaaaaaaasddfggdkfhndfre'; *indexOf('s')为数组编写该方法;indexOf('img/1.jpg'); */ window.onload = function(){ var arrImg = ['img/1.jpg','img/2.jpg','img/3.jpg','img/4.jpg','img/5.jpg','img/3.jpg']; var arr = [1,2,3,4,5,6,2]; /*由于IE8,不支持数组的indexOf*/ if (!Array.prototype.indexOf){ Array.prototype.indexOf = function(elt){ var len = this.length >>> 0; var from = Number(arguments[1] || 0); from = (from < 0) ? Math.ceil(from) : Math.floor(from); if (from < 0) from = 0;//如果第二个参数为负数,则按照0计算 for (; from < len; from++) { if (this[from] === elt) //没有必要用from in this 做越界检查 return from; } return -1; }; } alert(arr.indexOf(2,2)); alert(arrImg.indexOf('img/3.jpg',2)); }; </script> </head> <body> </body> </html>