javascript - 从数组动态构建表

标签 javascript jquery jquery-selectors

我已经尝试让这段代码工作几个小时了,似乎无法弄清楚我做错了什么或者我可能搞砸了什么,我从一个有效的示例页面复制了这段代码并修改它以制作我想要构建的表..

原始剪辑代码:

$(function () {
            var $wrap = $('<div>').attr('id', 'tableWrap');
            var $tbl = $('<table>').attr('id', 'basicTable');

            for (var i = 0; i < 200; i++) {
                $tbl.append($('<tr>').append($('<td>').text(i),$('<td>').text(200 - i)));
            }

            $wrap.append($tbl);
            $('body').append($wrap);
        });

现在我将其更改为六行,并从 javascript 数组加载链接和数据,但由于某种原因,它不会构建表格..

我的代码:

myArray = new Array();
    myArray[0] = new Array("001","Title","http://www.google.com/","https://www.google.com/images/logos/google_logo_41.png");
    myArray[1] = new Array("002","Title","http://www.google.com/","https://www.google.com/images/logos/google_logo_41.png");
    myArray[2] = new Array("003","Title","http://www.google.com/","https://www.google.com/images/logos/google_logo_41.png");
    myArray[3] = new Array("004","Title","http://www.google.com/","https://www.google.com/images/logos/google_logo_41.png");
    myArray[4] = new Array("005","Title","http://www.google.com/","https://www.google.com/images/logos/google_logo_41.png");
    myArray[5] = new Array("006","Title","http://www.google.com/","https://www.google.com/images/logos/google_logo_41.png");
    myArray[6] = new Array("007","Title","http://www.google.com/","https://www.google.com/images/logos/google_logo_41.png");
    myArray[7] = new Array("008","Title","http://www.google.com/","https://www.google.com/images/logos/google_logo_41.png");
    myArray[8] = new Array("009","Title","http://www.google.com/","https://www.google.com/images/logos/google_logo_41.png");
    myArray[9] = new Array("010","Title","http://www.google.com/","https://www.google.com/images/logos/google_logo_41.png");


    var $wrap = $('<div>').attr('id', 'tableWrap');              
    var $tbl = $('<table>').attr('id', 'basicTable'); 

    for(i=0;i<myArray.length;i++){
        $(function () {                                   
            $tbl.append($('<tr>').append(
                        $('<td>').text('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]'"></a>'),
                        $('<td>').text('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]'"></a>'),
                        $('<td>').text('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]'"></a>'),
                        $('<td>').text('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]'"></a>'),
                        $('<td>').text('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]'"></a>'),
                        $('<td>').text('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]'"></a>')));             
        }              
        $wrap.append($tbl);             
        $('#c').append($wrap);         
    });

我的 body 里有一个..

 <div id="c"></div>

我正在尝试将其加载到...我想坚持使用 jquery,但几乎想只使用 javascript document.write 函数

---- 更新 05/14/2012 ----
感谢许多人的帮助,现在只能使用 JavaScript。让它按照我现在需要的方式工作,直到我度假回来。我仍然想将它压缩/转换为 jquery,这样它看起来更干净并且已经将它用于其他事情。因此,对于任何读过这篇文章并且正在寻找这个方向的人来说,这里是 jsFiddle我折腾在一起,以帮助展示我如何在大家的帮助下进行设置......再次感谢大家!!

最佳答案

$(function () {      
    var $wrap = $('<div/>').attr('id', 'tableWrap');              
    var $tbl = $('<table/>').attr('id', 'basicTable');

    for(i=0;i<myArray.length;i++){
            //  $(function () { is miss placed                     
            $tbl.append($('<tr/>').append(
                        $('<td/>').html('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]+'"></a>'),
                        $('<td/>').html('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]+'"></a>'),
                        $('<td/>').html('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]+'"></a>'),
                        $('<td/>').html('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]+'"></a>'),
                        $('<td/>').html('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]+'"></a>'),
                        $('<td/>').html('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]+'"></a>')));             
        }              
        $wrap.append($tbl);             
        $('#c').append($wrap);         
    });

问题

  1. 缺少 $(function) {..
  2. 的位置
  3. alt="'+myArray[i][1]'" 应为 alt="'+myArray[i][1]+'"<

<强> DEMO

关于javascript - 从数组动态构建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10468724/

相关文章:

javascript - 如何使用 jQuery 从创建的弹出窗口重定向创建弹出窗口的页面?

javascript - 使用 ThreeJS 的 WebGL 中的水/镜面

jquery - Jasmine 没有加载供应商选项

javascript - 如何根据 Angular JS 中的另一个数组重新排序

jQuery 选择选项 - CodeIgniter

jquery - 如何将 jQuery 元素选择应用于字符串变量

javascript - jQuery keydown 创建列表

javascript - 在ajax函数中返回更少

javascript - typescript 将字符串解析为数字组 345-67 以及包含单词和数字的文本

jquery - 我如何处理 Jquery 中的选中事件和单击事件