日後如果要使用 array.indexOf ,要在呼叫前,先引用一段來處理支援各家瀏覽器的方法:
if(!Array.indexOf){ Array.prototype.indexOf = function(obj){ for(var i=0; i<this.length; i++){ if(this[i]==obj){ return i; } } return -1; } }
完整範例:
<html> <head><h1>JavaScript Array indexof </h1> <title>JavaScript array indexof </title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <script language="javascript" type="text/javascript"> var array=new Array(); array[0]="Rose"; array[1]="India"; array[2]=".net"; array[3]="Rohini"; document.write("<b>"+"Element in the array are "+"</b>"+ "<br />"); document.write("================="+ "<br />"); for(var i=0;i<array.length;i++){ document.write(array[i]+"<br>") } document.write("================="+ "<br />"); if(!Array.indexOf){ Array.prototype.indexOf = function(obj){ for(var i=0; i<this.length; i++){ if(this[i]==obj){ return i; } } return -1; } } var index = array.indexOf("India"); document.write("<b>"+"Index of India on the array is: "+"</b>"+index); </script> </body> </html>
http://www.roseindia.net/java/javascript-array/javascript-array-index-of.shtml
http://www.pearweb.com/javascript/array-index-of.html
沒有留言:
張貼留言