javascript - replaceWith 替换所有文本而不是仅空字符串

标签 javascript jquery html

所以我在替换我的 js/jQuery 代码上的空输出时遇到了一些麻烦,它正在替换每个文本,即使它是有效的。

没有 if (items[z].Office == null) { 语句:
enter image description here

使用 if 语句:
enter image description here

谁能告诉我我可能做错了什么?非常感谢所有帮助。

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/

相关文章:

javascript - JavaScript 循环中的访问范围

javascript - Ember - 通过商店或新的 ajax 调用手动链接到另一个模型

javascript - 如何将用户创建的复选框值传递到另一个页面?

javascript - 根据下拉菜单中的所选元素而变化的文本区域

javascript - 如何避免对同一组元素使用过滤器两次

javascript - 使用 TinyMCE 突出显示文本

jquery - 如何检索在日期选择器上在 fullcalendar 上创建的事件以管理 symfony 中的约会?

javascript - 如何让 PHP 验证回退?

javascript - 输入类型范围内的进度条

javascript - Canvas 外导航 : How to make a second-level menu slide in on top of first-level menu without covering body text?