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 />    
    鍥涖€佹暟缁勭殑澶勭悊
    

鎶€鏈垎浜? id=
<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>
View Code
鎶€鏈垎浜? id=
<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>
View Code

 

以上是关于js鏁扮粍鐨勫0鏄庝笌搴旂敤的主要内容,如果未能解决你的问题,请参考以下文章

leetcode 4. 瀵绘壘涓や釜姝e簭鏁扮粍鐨勪腑浣嶆暟

js 澶氫釜鏁扮粍鍙栧叾鐩稿悓鐨勫厓绱犲嵆瀛愰泦

浠嬬粛涓€涓瓧绗︿覆杞崲鐨勫嚱鏁扮粍SCMS_CONV

瀹屾暣妗堜緥鈥斺€旈厤缃墠绔拰鍚庣API搴旂敤鐨勫畨鍏ㄨ璇佲€斺€斿熀浜嶢zure瀹炵幇

javascript鍒犻櫎鏁扮粍鏌愪釜鍏冪礌