js鏁扮粍鐨勫0鏄庝笌搴旂敤
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js鏁扮粍鐨勫0鏄庝笌搴旂敤相关的知识,希望对你有一定的参考价值。
鏍囩锛?/p>
js鏁扮粍鐨勫0鏄庝笌搴旂敤
鏁扮粍锛氫竴绉嶅鍣紝瀛樺偍鎵归噺鏁版嵁銆?br />JavaScript鏄竴绉嶅急绫诲瀷璇█銆備粈涔堟槸寮辩被鍨嬶紝灏辨槸鍙橀噺鍙互瀛樺偍澶氱绫诲瀷鍙橀噺鐨勫紩鐢ㄤ笉浼氭姤閿欍€?br />鎵€浠s鏁扮粍鍙互瀛樺偍涓嶅悓鐨勬暟鎹€?br />
涓€銆佹暟缁勭殑浣滅敤:鍙鏄壒閲忕殑鏁版嵁閮介渶瑕佷娇鐢ㄦ暟缁勫0鏄?br />
浜屻€佸浣曞0鏄庢暟缁?br /> 1銆佸揩閫熷0鏄庢暟缁?br /> var 鏁扮粍鍚?[鍏冪礌1锛屽厓绱?锛?...];
2銆?br />
涓夈€侀亶鍘嗘暟缁?br />
鍥涖€佹暟缁勭殑澶勭悊
<script> /*蹇€熷0鏄庢暟缁?/span>*/ /* var names=["寮犱笁","鏉庡洓","鐜嬩簲"]; var arrs=[ ["111","222"], ["aaa","bbb"] ]; alert(names[1]); alert(arrs[1][1]); */ /* 瀵硅薄绫诲瀷鐨勫0鏄?*/ /* var p=new Object(); p.name="zhangsan"; p.age=10; p.sex="nan"; */ /* json琛ㄧず娉曪細 {灞炴€?锛氬€硷紝灞炴€?锛氬€? ...} var p={ "name":"zhangsan", "age":10, "sex":"nan" }; */ /* var p={ "name":"zhangsan", "age":10, "sex":"nan" }; alert(p.name); alert(p["name"]); */ //瀵硅薄鐨勮繖绉嶈〃绀烘柟寮忔槸涓嶆槸鍍忎竴涓暟缁勶紝鎵€浠ユ垜浠ā鎷熷嚭鏁扮粍鐨勪竴绉嶆柟寮忋€?/span> /*function myArray(){ var o=new Object(); o.length=arguments.length; if (o.length>0){ for(var i=0;i<arguments.length;i++){//杩欓噷瑕佹敞鎰忥紝濡傛灉灞炴€ф槸鍙橀噺涓嶅彲浠ヤ娇鐢?鐨勬柟寮忥紝涓€瀹氳浣跨敤[]灞炴€х殑鏂瑰紡鎵嶅彲浠? o[i]=arguments[i]; } } return o; } var myArray=new myArray(1,2,5,7,9); alert(myArray.length); alert(myArray[1]); */ /*鏃㈢劧杩炴垜浠兘鍙互妯℃嫙鍑轰竴涓暟缁勫璞★紝鏇翠綍鍐靛ぇ鐗涗滑锛屾墍浠avaScript涓烘垜浠彁渚涗簡涓€涓暟缁勫璞★細Array*/ /* var arr=new Array(10); alert(arr.length); alert(arr[1]);//鏁扮粍鐨勫厓绱犲鏋滄病鏈夊€硷紝榛樿鏄痷ndefined */ /*s Array瀵硅薄鐨勪竴浜涙柟娉曚笌灞炴€э細 灞炴€э細length 杩斿洖鏁扮粍鐨勯暱搴? 鏂规硶锛? concat([item1[, item2[, . . . [, itemN]]]]) 灏嗗涓暟缁勭粍鍚堟垚涓€涓暟缁? join(separator)杩斿洖鐢辨寚瀹氬垎鍓茬闅斿紑鐨勬暟缁勫唴瀹圭殑瀛楃涓插舰寮? reverse() 灏嗘暟缁勫厓绱犲弽杞紝閫嗗簭 slice(start[,end])鎴彇鏁扮粍銆傚鏋渆nd涓嶅~锛岃繑鍥炵殑鏄粠start涓嬫爣鍒扮粨灏剧殑鏁扮粍瀵硅薄銆傚鏋渆nd涓烘鏁帮紝鎴彇鐨勫氨鏄尯闂碵start,end),濡傛灉end鏄礋鏁帮紝琛ㄧず鍊掓暟绗嚑浣? splice(start, deleteCount, [item1[, item2[, . . . [,itemN]]]])浠庢暟缁勬寚瀹氫綅缃Щ闄ゆ寚瀹氶暱搴︾殑鍏冪礌锛屽彲浠ュ湪绉婚櫎鐨勪綅缃彃鍏ユ柊鍏冪礌锛岃繑鍥炴墍绉婚櫎鐨勫厓绱狅紝item1...鏄彲鍙樺弬 sort();鎺掑簭鏁扮粍鍏冪礌锛岄粯璁や娇鐢ㄨ嚜鐒舵帓搴忋€傚彲浠ヤ紶鍏ヤ竴涓瘮杈冩柟娉曪紝绫讳技浜巘reeMap涓殑姣旇緝鍣ㄥ璞? push(([item1 [item2 [. . . [itemN ]]]]))鍔犲叆鍏冪礌鍦ㄧ粨灏撅紝杩斿洖鏁扮粍鐨勯暱搴? pop() 灏嗘暟缁勮涓烘爤缁撴瀯锛屽垹闄ょ粨灏惧厓绱? shift()灏嗘暟缁勮涓洪槦鍒楃粨鏋勶紝鍒犻櫎绗竴涓竴涓厓绱犳槸浠庣涓€涓綅缃紑濮嬪垹闄? unshift([item1[, item2 [, . . . [, itemN]]]])锛屽弬鏁版槸鍙彉鍙傦紝娣诲姞鍏冪礌鍦ㄥ紑濮嬩綅缃紝椤哄簭涓庡弬鏁伴『搴忎竴鑷? */ //var arr1=[1,8,3,6,7]; //var arr2=[2,3,4,2]; //var arr3=arr1.concat(arr2); //alert(arr3); //alert(arr3.join("-")); //alert(arr1.reverse()); //alert(arr1.splice(1,2)); //alert(arr1.slice(2,3)); //alert(arr1.sort()); //渚濇嵁瀛楃涓查暱搴︽帓搴?/span> var arr1=["aa","aaaa","bbb","e","bbbbb"]; /* arr1.sort(function(a,b){ if(a.length>b.length){ return -1; }else if(a.length==b.length){ return 0; }else{ return 1; } }); alert(arr1); */ arr1.push("bk"); alert(arr1); arr1.pop(); alert(arr1); arr1.unshift("ss"); alert(arr1); arr1.shift(); alert(arr1); </script>
<html> <head> </head> <body> </body> </html> <script> //闇€姹傦細瀹炵幇璐悆铔囩殑鍦板浘浠ュ強铔囪捣鐐瑰拰椋熺墿鍧愭爣鐨勪綅缃?/span> /* 鍒嗘瀽锛? 鍦板浘鎬庝箞鍋氬憿锛? 浣跨敤div锛岀劧鍚庣粰鑳屾櫙棰滆壊锛宒iv鍥哄畾闀垮锛岃儗鏅紝杈规锛屽畾浣嶏紝浣滀负涓€涓牸瀛愩€傜敱澶氫釜鏍煎瓙缁勬垚鍦板浘 璧峰鏍煎瓙鍜岄鐗╂牸瀛愭€庝箞鍋氬憿锛? 涔熸槸div锛岃繖涓娇鐢ㄩ殢鏈烘暟锛屾墍鏈夌殑鏍煎瓙缁勬垚涓€涓簩缁存暟缁勩€傞殢鏈鸿幏鍙栦竴涓潗鏍囷紝鐒跺悗鑳屾櫙棰滆壊鏀瑰彉銆? */ var rows=20; var cols=20; var width=20; var height=20; var top=100; var left=500; //var bg="yellow"; var arr=new Array(rows); for(var i=0;i<arr.length;i++){ var temp=new Array(cols); for(var j=0;j<temp.length;j++){ temp[j]=0; } arr[i]=temp; } //alert(arr); var row1=random(rows); var col1=random(cols); arr[row1][col1]=1; do{ var row2=random(rows); var col2=random(cols); }while(row2==row1 || col2==col1) arr[row2][col2]=2; //alert(row1+"--"+col1); function random(a){ return parseInt(Math.random()*a); } for(var j=0;j<rows;j++){ for(var i=0;i<cols;i++){ if(arr[j][i]==1) var bg="red"; else if(arr[j][i]==2) var bg="blue"; else var bg="yellow"; document.write("<div style=鈥榖ackground-color:"+bg+";position:absolute;top:"+(100+j*height)+"px;left:"+(left+i*width)+"px;width:"+width+"px;height:"+height+"px;border: 1px white solid;鈥?gt;</div>"); } } </script>
以上是关于js鏁扮粍鐨勫0鏄庝笌搴旂敤的主要内容,如果未能解决你的问题,请参考以下文章
leetcode 4. 瀵绘壘涓や釜姝e簭鏁扮粍鐨勪腑浣嶆暟