Javascript 从数组动态生成表
我已经试着让这段代码运行了几个小时,但似乎不知道我做错了什么,或者我可能弄错了什么,我从一个有效的示例页面复制了这段代码,并对其进行了修改,以生成我想要构建的表 原始剪辑代码:Javascript 从数组动态生成表,javascript,jquery,jquery-selectors,Javascript,Jquery,Jquery Selectors,我已经试着让这段代码运行了几个小时,但似乎不知道我做错了什么,或者我可能弄错了什么,我从一个有效的示例页面复制了这段代码,并对其进行了修改,以生成我想要构建的表 原始剪辑代码: $(function () { var $wrap = $('<div>').attr('id', 'tableWrap'); var $tbl = $('<table>').attr('id', 'basicTable');
$(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);
});
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);
});
$(函数(){
var$wrap=$('').attr('id','tableWrap');
var$tbl=$('').attr('id','basicTable');
对于(变量i=0;i<200;i++){
$tbl.append($('').append($('').text(i),$('').text(200-i));
}
$wrap.append($tbl);
$('body')。追加($wrap);
});
现在我将其更改为六行,并从javascript数组加载链接和数据,但由于某些原因,它没有构建表
我的代码:
$(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);
});
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);
});
myArray=newarray();
myArray[0]=新数组(“001”,“Title”,”http://www.google.com/","https://www.google.com/images/logos/google_logo_41.png");
myArray[1]=新数组(“002”,“Title”,”http://www.google.com/","https://www.google.com/images/logos/google_logo_41.png");
myArray[2]=新数组(“003”,“Title”,”http://www.google.com/","https://www.google.com/images/logos/google_logo_41.png");
myArray[3]=新数组(“004”,“Title”,”http://www.google.com/","https://www.google.com/images/logos/google_logo_41.png");
myArray[4]=新数组(“005”、“Title”、”http://www.google.com/","https://www.google.com/images/logos/google_logo_41.png");
myArray[5]=新数组(“006”、“Title”、”http://www.google.com/","https://www.google.com/images/logos/google_logo_41.png");
myArray[6]=新数组(“007”,“Title”,”http://www.google.com/","https://www.google.com/images/logos/google_logo_41.png");
myArray[7]=新数组(“008”、“Title”、”http://www.google.com/","https://www.google.com/images/logos/google_logo_41.png");
myArray[8]=新数组(“009”、“Title”、”http://www.google.com/","https://www.google.com/images/logos/google_logo_41.png");
myArray[9]=新数组(“010”,“Title”,”http://www.google.com/","https://www.google.com/images/logos/google_logo_41.png");
var$wrap=$('').attr('id','tableWrap');
var$tbl=$('').attr('id','basicTable');
对于(i=0;i$(函数(){
var$wrap=$('').attr('id','tableWrap');
var$tbl=$('').attr('id','basicTable');
对于(i=0;i首先,您忘记在for
语句之后关闭table
元素
其次,为什么$(for
的之后的函数(){
是错误的。如果要使用此函数,则必须使用}将其关闭;
var$wrap=$('').attr('id','tableWrap');
var$tbl=$('').attr('id','basicTable');
对于(i=0;i);
我在这两个地方都试过了,但仍然无法加载…可能是阵列的吗?[link]啊,我讨厌错过了+符号上的小东西…啊,我看到了我错过的东西,但有一个问题,为什么必须在其中加载?…顺便说一句,谢谢你对我的帮助this@acrichm良好实践。您也可以使用u$(td)
/$(tr)
像这样,应该使用这些。k非常感谢这些提示,我一定会记住它们。感谢所有的帮助CodeParadox,这是我今天尝试的,只是想知道;为了在每个td中创建不同的图像,我拿出了除一美元以外的所有美元()…将for循环改为while(我在两个地方都试过,只是想看看这是否是它没有显示的原因,$tbl.append(“”;
认为我不必把它放在那里,jquery会帮我关闭它,因为这对我从中获取代码的这个网站有效。)
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">);