为什么我的风格没有在下面的jquery中应用。它只是在FireFox中添加表格
$("#advisorPerformance").append("<table class='mvc'><tr><th>Advisor</th><th>Packs In</th><th>Pack In (£)</th><th>Packs Out</th><th>Pack Out (£)</th><th>PDQs</th><th>PDQ (£)</th></tr>");
$.each(data.AdvisorPerformances, function(i) {
$("#advisorPerfomance").append("<tr>" +
"<td>" + data.AdvisorPerformances[i].Advisor +
"</td>" +
"<td>" + data.AdvisorPerformances[i].PackInCount +
"</td>" +
"<td>" + data.AdvisorPerformances[i].PacksInValue +
"</td>" +
"<td>" + data.AdvisorPerformances[i].PacksOutCount +
"</td>" +
"<td> " + data.AdvisorPerformances[i].PaymentsInCount +
"</td>" +
"<td>" + data.AdvisorPerformances[i].PaymentsInValue +
"</td>" +
"</tr>");
});
$("#advisorPerfomance").append("</table>");
$("#advisorPerfomance").addClass("NOTAPPLIEDSTYLE");
还有更好的方法来添加表吗?
答案 0 :(得分:1)
您希望使用.appendTo()
执行此类操作,以使其更快,更重要,工作:)
var tbl = $("<table class='mvc'><tr><th>Advisor</th><th>Packs In</th><th>Pack In (£)</th><th>Packs Out</th><th>Pack Out (£)</th><th>PDQs</th><th>PDQ (£)</th></tr></table>")
$.each(data.AdvisorPerformances, function(i) {
tbl.append("<tr>" +
"<td>" + data.AdvisorPerformances[i].Advisor + "</td>" +
"<td>" + data.AdvisorPerformances[i].PackInCount + "</td>" +
"<td>" + data.AdvisorPerformances[i].PacksInValue + "</td>" +
"<td>" + data.AdvisorPerformances[i].PacksOutCount + "</td>" +
"<td>" + data.AdvisorPerformances[i].PaymentsInCount + "</td>" +
"<td>" + data.AdvisorPerformances[i].PaymentsInValue + "</td>" +
"</tr>");
});
tbl.appendTo("#advisorPerformance");
$("#advisorPerfomance").addClass("NOTAPPLIEDSTYLE");
a.append()
不只是附加文字并将其设为html,而是创建DOM元素,因此您的第一行目前正在创建一个未关闭的<table>
...所以从那时起你就会拥有不可预知的结果。相反,要么添加完整的有效元素,要么像我上面那样构建你的表,然后将它添加到DOM中。上面的方法在文档片段中创建DOM外部的表然后附加它,这更快,更清洁。完成后,它会将整个表格添加到#advisorPerformance
。
答案 1 :(得分:0)
我注意到你的一些代码使用“advisorPerfomance”,而其他代码使用“advisorPerformance”。我猜你正在尝试“addClass”的JQuery选择器会产生一个零长度的数组,因此它将该类添加到没有元素。
简而言之,始终确保选择器返回您认为正在选择的内容。