gpt4 book ai didi

javascript - $.each() jQuery 新闻源

转载 作者:行者123 更新时间:2023-12-02 16:04:01 27 4
gpt4 key购买 nike

我正在尝试在我的网站上运行新闻源。它显示所有新闻并循环播放除过期新闻外的所有新闻。我可以通过网页添加新闻,因此 JSON 可能会不断变化。 PHP 脚本生成 JSON 如下:

[
{
"0": "Monsieur",
"1": "X",
"2": "24",
"3": "236",
"4": "Artichauts",
"5": "4",
"6": "2015-06-19",
"7": "2015-06-26",
"8": "9",
"9": "7",
"nom": "Monsieur",
"prenom": "X",
"id_promotion": "24",
"id_commercant": "236",
"article": "Artichauts",
"rubrique": "4",
"date_debut": "2015-06-19",
"date_fin": "2015-06-26",
"prix_origine": "9",
"prix_promotion": "7"
},
{
"0": "Monsieur",
"1": "X",
"2": "23",
"3": "236",
"4": "Betteraves",
"5": "4",
"6": "2015-06-18",
"7": "2015-06-25",
"8": "8",
"9": "6",
"nom": "Monsieur",
"prenom": "X",
"id_promotion": "23",
"id_commercant": "236",
"article": "Betteraves",
"rubrique": "4",
"date_debut": "2015-06-18",
"date_fin": "2015-06-25",
"prix_origine": "8",
"prix_promotion": "6"
}
]

我的 jQuery 脚本正在(尝试)展示它们

$(document).ready(function () {
function newsfeed() {
$.getJSON("newsfeed.php", function (result) {
var htmldata = "";
$.each(result, function (key) {
$("#news").html(result[key].nom + " " + result[key].prenom + " has a special offer : " + result[key].article + " at " + result[key].prix_promotion + "€ instead of " + result[key].prix_origine + "€ ").delay(5000).slideUp(300)
.delay(500)
.fadeIn(400);
});
x = setTimeout(function () {
newsfeed()
}, 5000);
});
}
newsfeed();
});

所以这段代码不断输出

Monsieur X has a special offer : Betteraves at 6€ instead of 8€

所以我的问题是, $.each() 函数没有按我想要的方式工作。如果有人有想法,我将不胜感激。

最佳答案

循环内的

$("#news").html 正在用循环元素替换 #news 的全部内容。

each中创建所有html,并在完成循环后将其注入(inject)到div中。

    var htmldata = '' ;
$.each(result, function(key){
htmldata += result[key].nom + " " + result[key].prenom + " has a special offer : " + result[key].article + " at " + result[key].prix_promotion + "€ instead of " + result[key].prix_origine + "€ ";
});
$('#news').html(htmldata);

关于javascript - $.each() jQuery 新闻源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30934477/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com