我试图只显示一个标题
s ='<div class="title ' + summaryClass +'">' + summary + '</div>';
来自每个循环内部,但每次循环时都会显示,因为你可以猜到我对这个lol的新内容
$.each(data.items, function(e, item) {
var starting = item.start.dateTime || item.start.date ||'';
var end = item.end.dateTime || item.end.date ||'';
var summary = item.summary || '';
var description = item.description;
var location = item.location;
var theDay = item.start.dateTime || item.start.date ||'';
var summaryClass = summary.toLowerCase().replace(/\s+/g,"-");
s ='<div class="title ' + summaryClass +'">' + summary + '</div>';
/*s +='<div class="day">'+ formatDate(theDay, defaults.dateFormating.trim()) + '</div>';*/
s +='<div class="time">'+ formatDate(starting, defaults.dateFormat.trim()) +' - '+ formatDate(end, defaults.dateFormat.trim()) +'</div>';
s +='<div class="time"></div>';
if(description) {
s +='<div class="description">'+ description + '</div>';
}
if(location) {
s +='<div class="location">Location: ' + location + '</div>';
}
if(formatDate(theDay, defaults.dateFormating.trim()) == "Saturday") {
$($div).append('<li class="' + formatDate(theDay, defaults.dateFormating.trim()) + '">' + s + '</li>');
}
});
答案 0 :(得分:0)
据我了解,您只想展示一次标题。
那你为什么不设置一个变量来检查你是否已经显示了标题呢?
e.g。 var titleShown = false
Here就是我所说的一个小例子。
var titleShown = false;
$.each(_array, function(e,item){
if(!titleShown)
{
result += "Title: " + item.title + "; ";
titleShown= true;
}
答案 1 :(得分:0)
我已经更新了你的代码,只需检查一下。我是否检查tag(id)是否存在。第一次迭代它将为null,一旦执行标记,第二次迭代它将存在,因此它将跳过标记。
$.each(data.items, function(e, item) {
var starting = item.start.dateTime || item.start.date ||'';
var end = item.end.dateTime || item.end.date ||'';
var summary = item.summary || '';
var description = item.description;
var location = item.location;
var theDay = item.start.dateTime || item.start.date ||'';
var summaryClass = summary.toLowerCase().replace(/\s+/g,"-");
if(document.getElementById("check_once") == null){
s ='<div id="check_once" class="title ' + summaryClass +'">' + summary + '</div>';
}
/*s +='<div class="day">'+ formatDate(theDay, defaults.dateFormating.trim()) + '</div>';*/
s +='<div class="time">'+ formatDate(starting, defaults.dateFormat.trim()) +' - '+ formatDate(end, defaults.dateFormat.trim()) +'</div>';
s +='<div class="time"></div>';
if(description) {
s +='<div class="description">'+ description + '</div>';
}
if(location) {
s +='<div class="location">Location: ' + location + '</div>';
}
if(formatDate(theDay, defaults.dateFormating.trim()) == "Saturday") {
$($div).append('<li class="' + formatDate(theDay, defaults.dateFormating.trim()) + '">' + s + '</li>');
}
})