1. [代码] [JavaScript]代码 !doctype htmlhtmlheadmeta charset="utf-8"title匿名函数浏览器检测案例/title/headbodyscriptfunction showTable(){var r=2;if(window.ActiveXObject){ //判断是否为ie浏览器 window.ActiveXObject为真
1. [代码][JavaScript]代码
<!doctype html> <html> <head> <meta charset="utf-8"> <title>匿名函数浏览器检测案例</title> </head> <body> <script> function showTable(){ var r=2; if(window.ActiveXObject){ //判断是否为ie浏览器 window.ActiveXObject为真 则是ie浏览器 r=1; } if(r==1){ showTable=function(){ //ie用table var arr=arguments[0]; document.write("<table border='1' width='800px'>"); for(var i=0;i<arr.length;i++){ document.write("<tr>"); document.write("<td>"+arr[i].name+"</td><td>"+arr[i].age+"</td>"); document.write("</tr>"); } } }else if(r==2){ showTable=function(){ //ff用ul var arr=arguments[0]; document.write("<ul>"); for(var i=0;i<arr.length;i++){ document.write("<li>"); document.write(arr[i].name+" "+arr[i].age); document.write("</li>"); } document.write("</ul>") } } } function showTree(){ var r=2; if(window.ActiveXObject){ r=1; } if(r==1){ showTree=function(){ document.write("以ie的特性来显示tree<br/>"); } }else if(r==2){ showTree=function(){ document.write("以ff的特性来显示tree<br/>"); } } } (function(){ showTable(); showTree(); })(); var arr=[{name:'smith',age:20},{name:'tom',age:22}]; showTable(arr); </script> </body> </html>