我将此作为我的代码,但我无法将其输出。它应该输出到
var guestList = [{
name: "Alex",
address: "283 Happy Lane"
}, {
name: "Avery",
address: "32 Skippy Skip"
}, {
name: "Pat",
address: "666 Heaven St."
}, {
name: "Bobbie",
address: "83 Quality St."
}, {
name: "Parker",
address: "92 Batty Bat"
}];
$(document).ready(function () {
for (pointer = 0; pointer < guestList.length; pointer++) {
labelHtml =
"<div class='label'>" + "<p>" + guestList[pointer].name + "</p>" + "<p>" + guestList[pointer].address + "</p>" + "</div>";
$("body").append(labelHtml);
}
});
答案 0 :(得分:0)
这段代码对我有用,这让我问你两个愚蠢的问题:
我很抱歉,但这只能是这两件事之一。
答案 1 :(得分:0)
几乎没有更正:
pointer
变量未定义。labelHtml
。更新了脚本:
$(document).ready(function () {
var guestList = [
{
name: "Alex",
address: "283 Happy Lane"
}, {
name: "Avery",
address: "32 Skippy Skip"
}, {
name: "Pat",
address: "666 Heaven St."
}, {
name: "Bobbie",
address: "83 Quality St."
}, {
name: "Parker",
address: "92 Batty Bat"
}];
for (var pointer = 0; pointer < guestList.length; pointer++) {
labelHtml +=
"<div class='label'>" + "<p>" + guestList[pointer].name + "</p>" + "<p>" + guestList[pointer].address + "</p>" + "</div>";
$("body").append(labelHtml);
}
});
答案 2 :(得分:0)
此代码实际上有效!见这里:http://jsfiddle.net/c7W7C/
您是否正确包含了jQuery?尝试:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
答案 3 :(得分:0)
如果您使用的是jquery-mobile ,则不应使用$(document).ready()
,因为jqm正在加载带有ajax的网页,并且无法保证$(document).ready()
在此处正常工作上下文。请参阅JQM documentation。
尝试.on('pageinit'...)
或.on('pageshow'...)
代替(根据您的需要):
$(document).on('pageinit', function () {
var guestList = [
{
name: "Alex",
address: "283 Happy Lane"
}];
for (var pointer = 0; pointer < guestList.length; pointer++) {
labelHtml +=
"<div class='label'>" + "<p>" + guestList[pointer].name + "</p>" + "<p>" + guestList[pointer].address + "</p>" + "</div>";
$("body").append(labelHtml);
}
});
也许你必须在将内容添加到dom后增强内容。