7.17.2013

How to declare a Two-Dimensional Array with the jQuery (II)

哼,不想再寫那些龞腳的英文了!! 前兩篇文章不知道自己發了什麼瘋,居然想要用英文來表達,表達到最後發現自己又沒那個能力....>"<

這一篇文章延續上一篇,在講如何使用jQuery來宣告一個二維陣列;前一篇已經知道一維陣列是如何產生的,接下來我們就可以產生出二維陣列了:
var tempArray = function(ROW, COL) {

    var virArray = new Array(ROW);
    $.each(virArray, function(index1, context1) {
        virArray[index1] = new Array(COL);
        $.each(virArray[index1], function(index2, context2) {
            virArray[index1][index2] = "Hello!! jQuery!! \n";
            return;
        });
        return;
    });
    return virArray;
};
當然,我們能夠產生二維陣列,也要能夠讀取;以下的範例,我們要產生一個4 * 2大小的二維陣列,陣列的內容均為一個字串:"Hello!! jQuery!! \n",並且將它們顯示在網頁上:
<!DOCUMENT html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.prg/TR/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="./js/jQuery/jquery-1.8.3.min.js"></script>
<script type="text/javascript" language="JavaScript">
// 宣告使用jQuery來初始化一個二維陣列
var tempArray = function(ROW, COL) {
    var virArray = new Array(ROW);
    $.each(virArray, function(index1, context1) {
        virArray[index1] = new Array(COL);
        $.each(virArray[index1], function(index2, context2) {
            virArray[index1][index2] = "Hello!! jQuery!! \n";
            return;
        });
        return;
    });
    return virArray;
};
</script>
</head>
<body>
    <div id="DebugConsole"></div>
</body>
<script type="text/javascript">
    (function($) {
        $(window.document).ready(function() {
            var strTemp = null;
            // 將二維陣列裡的資料內容一一地讀出來
            $.each(tempArray(4, 2), function(index1, context1) {
                $.each(context1, function(index2, context2) {
                    strTemp = "tempArray(4, 2)[" + index1 + "][" + index2 + "]: " + context2 + "<br/>";
                    $("#DebugConsole").append(strTemp);
                    return;
                });
                $("#DebugConsole").append("<br/>");
                return;
            });
            return;
        });
        return;
    })(jQuery);
</script>
</html>
執行結果:


相關文章:
How to declare a Two-Dimensional Array with the jQuery (I)

沒有留言:

張貼留言