0

我一直试图让这段代码工作几个小时,但似乎无法弄清楚我做错了什么或我可能搞砸了,我从一个有效的示例页面复制了这段代码并对其进行了修改制作我想要建造的桌子..

原始剪辑代码:

$(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);         
    });

在我的身体里有一个..

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

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

---- 2012 年 5 月 14 日更新 ----
多亏了很多人的帮助,现在只能使用 JavaScript。让它按照我现在的需要工作和做,直到我从假期回来。我仍然想将它压缩/转换为 jquery,所以它看起来更干净并且已经将它用于其他事情。因此,对于任何遇到过这篇文章并且正在寻找这个方向的人来说,这里的jsFiddle我一起扔来帮助展示我是如何在每个人的帮助下设置它的......再次感谢大家!

4

2 回答 2

1
$(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. alt="'+myArray[i][1]'"应该alt="'+myArray[i][1]+'"

演示

于 2012-05-06T07:02:01.797 回答
1

首先,您忘记在声明table后关闭元素for

第二,为什么是$(function() {之后for?是错的。如果要使用这个,你必须用});

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

    for(i=0;i<myArray.length;i++){                                 
            $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>')));             
        }              
  $tbl.append("</table">);
于 2012-05-06T07:03:18.860 回答