【发布时间】:2011-01-09 03:48:20
【问题描述】:
下面的代码有问题:
function showTableData()
{
var tableArray;
var x = 0;
var theHTML;
for (i = 0; i < 7032; i++)
{
if (x = 0)
{
theHTML = '<tr>' +
'<th scope="row" class="spec">' + partNum[i] + '</th>' +
'<td>' + Msrp[i] + '</td>' +
'<td>' + blah[i] + '</td>' +
'<td>' + blahs[i] + '</td>' +
'</tr>' + theHTML;
x++;
}else{
theHTML = '<tr>' +
'<th scope="row" class="specalt">' + partNum[i] + '</th>' +
'<td class="alt">' + Msrp[i] + '</td>' +
'<td class="alt">' + blah[i] + '</td>' +
'<td class="alt">' + blahs[i] + '</td>' +
'</tr>' + theHTML;
x--;
}
}
theHTML = '<table id="mytable" cellspacing="0">' +
'<tr>' +
'<th scope="col" abbr="Configurations" class="nobg">Part Number</th>' +
'<th scope="col" abbr="Dual 1.8">Msrp Price</th>' +
'<th scope="col" abbr="Dual 2">blahs Price</th>' +
'<th scope="col" abbr="Dual 2.5">Low Price</th>' +
'</tr>' + theHTML + '</table>';
$('#example').append(theHTML);
}
</script>
<div id="example">
</div>
问题在于 $('#example').append(theHTML); 永远不会执行(或显示在页面上)。我认为这是因为字符串太长了!它在数组中有超过 7,000 个项目,所以我不确定这是原因还是其他原因?
任何帮助都会很棒!谢谢!
大卫
【问题讨论】:
-
if (x=0)(第 9 行)应该是if (x==0)不知道这是否只是一个错字 -
你确定函数调用正确吗?
-
一些建议。使用
push而不是字符串连接来构建一个数组。然后在附加 html 之前使用array.join('')。更好的性能。 -
谢谢,铁杉。修复了该错误。
标签: javascript jquery arrays performance