javascript - 如果第二条是黑色的,如何只发送一条消息,但如果我填写了两个变量,如何发送两条消息?
问题描述
我根据我在按钮中输入的消息在 div 中添加段落标签。其中一个按钮 (HUNT) 包含两条消息,而第二个按钮 (SELL) 仅包含一条消息。我遇到的问题是,无论第二条消息是否为空白,我总是会显示“未定义”消息。
这是我的代码:
function addLog(logBefore, logAfter) {
var par = document.createElement("p");
var node1 = document.createTextNode(logBefore);
var node2 = document.createTextNode(logBefore);
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 >= 8) {
element.removeChild(element.childNodes[0]);
}
element.appendChild(par);
if (node2 != '') {
setTimeout(function addLog(logBefore, 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);
};
};
var credits = 0;
var clickPower = 1;
function addCred() {
credits = credits + clickPower;
document.getElementById('credits').innerHTML = credits + " Skatts";
};
#logs {
display: flex;
flex-direction: column-reverse;
}
#logs p {
margin-top: 0px;
}
<div id="credits"></div>
<button onclick="addLog('Hunt begun', 'Hunt successful! You now have ' + credits + ' Skatts'); addCred();">HUNT</button>
<br>
<button onclick="addLog('Resources sold', '')">SELL</button>
<div id="logs"></div>
解决方案
我对您的代码以及它要做的事情有点困惑,但我相信:
您不需要像这样调用超时函数:
setTimeout(function addLog(logBefore, logAfter) {
为什么甚至再次将其
addLog
称为与父母相同的名称?而且您不需要在其中传递值,因为它是可以使用您的父值的嵌套函数logBefore
anlogAfter
,所以只需执行以下操作:setTimeout(function () {
现在,当您在 timeout 内控制台日志值时,您将看到传递的值nod2
并将被填充。
function addLog(logBefore, logAfter) {
var par = document.createElement("p");
var node1 = document.createTextNode(logBefore);
var node2 = document.createTextNode(logBefore);
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 >= 8) {
element.removeChild(element.childNodes[0]);
}
element.appendChild(par);
if (node2 != '') {
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);
};
};
var credits = 0;
var clickPower = 1;
function addCred() {
credits = credits + clickPower;
document.getElementById('credits').innerHTML = credits + " Skatts";
};
#logs {
display: flex;
flex-direction: column-reverse;
}
#logs p {
margin-top: 0px;
}
<div id="credits"></div>
<button onclick="addLog('Hunt begun', 'Hunt successful! You now have ' + credits + ' Skatts'); addCred();">HUNT</button>
<br>
<button onclick="addLog('Resources sold', '')">SELL</button>
<div id="logs"></div>
推荐阅读
- amazon-web-services - 在 SQS + Lambda 中,我可以编写从 SQS 读取消息的 Lambda 内部轮询器的分布式处理逻辑吗?
- python - 将透明图像导入 GAN
- java - 如何在没有自签名证书的情况下将 SSL 证书与 ActiveMQ 一起使用
- node.js - “找不到模块 npm-cli.js”
- slack - Slack 机器人可以自动加入频道吗?
- sql - 触发器在另一个表中创建或更新记录
- kubernetes - 有没有办法为 CPU pod 禁用 GPU?
- python - 如何摆脱异常类:NoneType?
- java - 如何始终使用 HttpServletRequestWrapper?
- mongodb - 使用 Mongoose 在 MongoDB 中查询多个属性