如何让我的消息正确显示?

时间:2021-04-18 20:40:03

标签: javascript html

我有两个按钮 - 一个以一秒的间隔发送两条消息,另一个只发送一条消息,第二个未定义(看到代码后会更清楚)。如何阻止 undefined 出现?

var credits = 0;
var currency = 'Skatter';

function addLog(logBefore, logAfter) {
  var par = document.createElement("p");
  var node1 = document.createTextNode(logBefore);
  var node2 = document.createTextNode(logAfter);
  par.appendChild(node1);

  var element = document.getElementById("logs");
  // Here you can also use element.childNodes.length
  const count = document.getElementById("logs").getElementsByTagName("p").length;
  if (count >= 18) {
    element.removeChild(element.childNodes[0]);
  }
  element.appendChild(par);

  if (node2 !== undefined) {
    setTimeout(function() {

      console.log(logBefore)
      console.log(logAfter)

      var par = document.createElement("p");
      var node2 = document.createTextNode(logAfter);
      par.appendChild(node2);


      var element = document.getElementById("logs");
      // Here you can also use element.childNodes.length
      const count = document.getElementById("logs").getElementsByTagName("p").length;
      if (count >= 8) {
        element.removeChild(element.childNodes[0]);
      }
      element.appendChild(par);
    }, 1000);
  }
};
<button onclick="addLog('Hunt begun', 'Hunt successful! You now have ' + credits + ' ' + currency)">HUNT</button>
<br>
<button onclick="addLog('Resources sold')">SELL</button>

<div id="logs" style="display: flex; flex-direction: column-reverse;"></div>

3 个答案:

答案 0 :(得分:1)

在第二个按钮上,您只向 addLog 函数传递一个参数。

答案 1 :(得分:1)

您的代码的核心问题是您询问您创建的 library(dplyr) a %>% group_by(x) %>% mutate(y_comb=paste(as.character(sort(unique(y))))) %>% slice(1) %>% ungroup() 变量是否为假(评估为假),但文本节点永远不会评估为假,因为它是一个保存值的对象< /p>

node2

如果您想检查程序是否为第二个参数 (// try this in your browser console >>> if (document.createTextNode(undefined)) { console.log("not null") } >>> "not null" //output ) 提供了值,那么您应该明确地执行此操作,例如:

logAfter

答案 2 :(得分:0)

您应该更改条件以检查直接日志 logAfter 是否未定义,而不是 textNode。这将解决您的问题...

var credits = 0;
var currency = 'Skatter';

function addLog(logBefore, logAfter) {
  var par = document.createElement("p");
  var node1 = document.createTextNode(logBefore);
  var node2 = document.createTextNode(logAfter);
  par.appendChild(node1);

  var element = document.getElementById("logs");
  // Here you can also use element.childNodes.length
  const count = document.getElementById("logs").getElementsByTagName("p").length;
  if (count >= 18) {
    element.removeChild(element.childNodes[0]);
  }
  element.appendChild(par);

  if (logAfter !== undefined) {
    setTimeout(function() {


      var par = document.createElement("p");
      var node2 = document.createTextNode(logAfter);
      par.appendChild(node2);


      var element = document.getElementById("logs");
      // Here you can also use element.childNodes.length
      const count = document.getElementById("logs").getElementsByTagName("p").length;
      if (count >= 8) {
        element.removeChild(element.childNodes[0]);
      }
      element.appendChild(par);
    }, 1000);
  }
};
<button onclick="addLog('Hunt begun', 'Hunt successful! You now have ' + credits + ' ' + currency)">HUNT</button>
<br>
<button onclick="addLog('Resources sold')">SELL</button>

<div id="logs" style="display: flex; flex-direction: column-reverse;"></div>