所以我在替换我的 js/jQuery 代码上的空输出时遇到了一些麻烦,它正在替换每个文本,即使它是有效的。
没有 if (items[z].Office == null) {
语句:
谁能告诉我我可能做错了什么?非常感谢所有帮助。
for(z = 0; z < items.length; z++)
{
var team = '<div class="row"><div class="col-sm-8"><p style="margin-bottom: 0px;">' + items[z].TeamName + '</p></div><div class="col-sm-4"><p class="office" style="margin-bottom: 0px;">' + items[z].Office + '</p></div></div>';
var single = '<div class="row"><div class="col-sm-8"><p style="margin-bottom: 0px;">' + items[z].Title + '</p></div><div class="col-sm-4"><p class="office" style="margin-bottom: 0px;">' + items[z].Office +
'</p></div></div>';
if (items[z].ApplicationType == 'OneDrive') {
onedriveSessions++; // Used to pull in Sessions
}
if (items[z].Office != 'None' && items[z].TeamName != 'None') {
switch (items[z].ApplicationType) {
case "OneDrive":
$(".onedriveTeams").append(team);
break;
}
if (items[z].RequestType == 'Single User') {
switch (items[z].ApplicationType) {
case "OneDrive":
$(".onedriveSingles").append(single);
break;
}
if (items[z].Office == null) {
$(".office").replaceWith("No office");
}
}
最佳答案
'<p class="office" style="margin-bottom: 0px;">' + items[z].Office + '</p>'
这里的逻辑不是插入null,而是放入一个默认值。
'<p class="office" style="margin-bottom: 0px;">' + (items[z].Office || 'No office') + '</p>'
那么您就不必在事后修复它。
关于javascript - replaceWith 替换所有文本而不是仅空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56484548/