我试图像这样循环遍历div内的数据集合,但是却收到错误消息,提示未终止的字符串文字。以引号(“)开头的字符串必须在行尾之前终止。我该如何解决?
<a href="@(eventItem.Url)" class="media-box__cover">
@if (listingImageUrl == "") {
if (currentCount % 2 == 0) {
<div class="media-box__cover-image" style="background-color: #ffd205;"></div>
}else {
<div class="media-box__cover-image" style="background-color: #0d9430;"></div>
}
} else {
<div class="media-box__cover-image" style="background-image: url('@(listingImageUrl)');"></div>
}
</a>
对此,
for (i = 0; i < json["data"].length ; i++) {
if(typeof(json["data"][i]) == 'object' && json["data"][i] != null){
htmlRetStr = "<div class='block'><div class='media-boxes'>";
htmlRetStr += "<div class='media-box'>";
htmlRetStr += "<a href=\""+ json["data"][i]["eventUrl"] +"\" class='media-box__cover'>";
htmlRetStr += "@if (json["data"][i]["eventUrl"] == '') { "; //error here
htmlRetStr += "if (currentCount % 2 == 0) {";
htmlRetStr += "<div class='media-box__cover-image' style='background-color: #ffd205;'></div>";
htmlRetStr += "}else{";
htmlRetStr += "<div class='media-box__cover-image' style='background-color: #0d9430;'></div> }";
htmlRetStr += " } else {";
htmlRetStr += "<div class='media-box__cover-image' style='background-image: url(\""+ json["data"][i]["listingImage"] +"\");'></div>} </a>";
htmlRetStr += "<div class='media-box__main'><div class='media-box__main-inner'><div class='media-box__header'>";
htmlRetStr += "<h3 class='media-box__heading'><a href=\""+ json["data"][i]["eventUrl"] +"\" class='media-box__heading-link'>" + json["data"][i]["heading"] + "</a></h3>";
htmlRetStr += "<div class='media-box__meta'>Posted on: " + json["data"][i]["articleDate"] +" "+ json["data"][i]["articleMonth"] +" "+ json["data"][i]["articleYear"] + "</div></div>";
htmlRetStr += "<p>"+ json["data"][i]["subHeading"] +"…</p>";
htmlRetStr += "</div></div></div></div></div>";
$("#news").append(htmlRetStr);
}
答案 0 :(得分:0)
不确定这是否有帮助,但希望这是您想要的。您可能需要检查一下有关 currentCount 的一件事。这是来自JSON还是其他任何地方。
for (i = 0; i < json["data"].length; i++) {
if (typeof(json["data"][i]) == 'object' && json["data"][i] != null) {
htmlRetStr = "<div class='block'><div class='media-boxes'>";
htmlRetStr += "<div class='media-box'>";
htmlRetStr += "<a href=\"" + json["data"][i]["eventUrl"] + "\" class='media-box__cover'>";
if (json["data"][i]["eventUrl"] == '') {
if (currentCount % 2 == 0) {
htmlRetStr += "<div class='media-box__cover-image' style='background-color: #ffd205;'></div>";
} else {
htmlRetStr += "<div class='media-box__cover-image' style='background-color: #0d9430;'></div>";
}
} else {
htmlRetStr += "<div class='media-box__cover-image' style='background-image: url(\"" + json["data"][i]["listingImage"] + "\");'></div>} </a>";
}
htmlRetStr += "<div class='media-box__main'><div class='media-box__main-inner'><div class='media-box__header'>";
htmlRetStr += "<h3 class='media-box__heading'><a href=\"" + json["data"][i]["eventUrl"] + "\" class='media-box__heading-link'>" + json["data"][i]["heading"] + "</a></h3>";
htmlRetStr += "<div class='media-box__meta'>Posted on: " + json["data"][i]["articleDate"] + " " + json["data"][i]["articleMonth"] + " " + json["data"][i]["articleYear"] + "</div></div>";
htmlRetStr += "<p>" + json["data"][i]["subHeading"] + "…</p>";
htmlRetStr += "</div></div></div></div></div>";
$("#news").append(htmlRetStr);
}
}