简体   繁体   English

如何使div充当控制台?

[英]How to make div act as console?

Wanted to ask how to make a div act as a console. 想问一下如何让div充当控制台。 For example code: 例如代码:

  buyBank: function(){ if(this.cash>(1+this.banks)*10){ this.cash -= (1+this.banks)*10; this.banks++; amount3.innerHTML = this.banks; price3.innerHTML = "$"+(1+this.banks)*10; logger.innerHTML = "Bank nupirktas!"; }else{ console.log("Nepakanka pinigu bankui!"); } } buy3btn.addEventListener("click", function(){ game.buyBank(); }); 

What id does now, if statement = true, it writes "Bank nupirktas" to a div (logger). 现在,id会执行什么操作,如果语句= true,它将“ Bank nupirktas”写入div(记录器)。 However, on second click nothing changes (probably posts to the same line). 但是,在第二次单击上没有任何更改(可能发布到同一行)。 I want it to break line and post a new one on every click (while keeping the old ones). 我希望它能换行并在每次单击时都发布一个新的(同时保留旧的)。

That's because you're overwriting the previous content. 那是因为您要覆盖以前的内容。

 var div = document.querySelector('div'); div.innerHTML = 'You will never see me'; div.innerHTML = 'Because I overwrite it'; 
 <div></div> 

Instead, you need to append content to it. 相反,您需要向其附加内容。

 function log(msg) { var div = document.querySelector('div'); div.innerHTML += msg + '<br />'; // Add a line break } log('First line.'); log('Second line.'); log('Third line.'); 
 <div></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM