这是我的jquery代码
$("document").ready(function() {
$.getJSON("http://bluewingholidays.com/results.json", function(data) {
$("#div-my-table").text("<table>");
$.each(data, function(i, item) {
$("#div-my-table").append("<tr><td>" + item.EncoderName + "</td><td>" + item.EncoderStatus + "</td></tr>");
});
$("#div-my-table").append("</table>");
});
});我想使用html表格将数据显示到web中。
<table id="div-my-table">
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
</table>但是什么都没发生?
发布于 2012-12-11 04:12:51
我假设您的表已经存在,所以这应该是可行的:
<table id="div-my-table"> </table>并在脚本中处理返回的JSON:
$.each(data.Properties, function(i, item) {
$("#div-my-table").append("<tr><td>" + item.id + ":" + item.title + "</td><td>" + item.price + "</td></tr>");
});发布于 2012-12-11 03:13:03
我马上发现的一个问题是,您需要将$("document")更改为$(document)。您希望传递document对象,而不是选择器。
$(document).ready(function(){...发布于 2012-12-11 03:21:43
append不会在jQuery中附加一些任意文本(尤其不是</table>)!它附加一个元素..。你应该使用这样的代码:
// Content will contain the HTML code of your new table
var content = "";
$.each(data, function(i, item) {
content += "<tr><td>" + item.EncoderName + "</td><td>" + item.EncoderStatus + "</td></tr>";
});
// Set the HTML of your table to this new content
$("#div-my-table").html(content);https://stackoverflow.com/questions/13807523
复制相似问题