简体   繁体   English

javascript-如何在javascript之上创建html标签?

[英]How to create an html tag above others in javascript?

So I'm attempting to make this thing where, when you press a button, it'll add a line of text to a scroll-able div. 因此,我试图将其制作成在按下按钮时会在可滚动div中添加一行文本的地方。 However, every time I press the button, the new paragraph appears below the last one, when I want it to appear above the last one. 然而,每一次我按下按钮时,新的段落显示下方是最后一个,我希望它出现上述最后一个。 I've got this down so far: 到目前为止,我已经掌握了以下内容:

 function addToLog(user, action) { // Setting up local variables var newEntry = document.createElement("p"); var entryText; var battleLog = document.getElementById("log"); // Applying style newEntry.style.color = "black"; newEntry.style.margin = 0; // Checking possible inputs if (user === 'player' && action === 'attack') { entryText = document.createTextNode("Player attacked the enemy for some damage!"); } else { entryText = document.createTextNode("Error"); } // Placing text into log newEntry.appendChild(entryText); battleLog.appendChild(newEntry); }; 
 #log { background-color: lightgray; height: 300px; width: 900px; overflow-y: scroll; } #attack:hover { color: red; } .button { border: 2px solid white; background-color: gray; height: 16px; width: 75px; text-align: center; display: inline-block; } .button:hover { color: black; font-weight: bold; } 
 <!DOCTYPE html> <html> <head> </head> <body> <div id="attack" ; class="button" onClick="addToLog('player','attack')">Attack</div> <!-- Displaying action log --> <div id="log"></div> <!-- Starting game script --> <script type="text/javascript" ; src="gameScript.js"></script> </body> </html> 

I ended up copy-pasting parts of my current project into here, so it might look messy. 我最终将当前项目的一部分粘贴到此处,因此看起来可能很混乱。 As you can see, when you click the attack button, the text appears in the action log. 如您所见,当您单击“攻击”按钮时,文本将显示在操作日志中。 However, every new paragraph appears at the very bottom of the action log, when I want it to appear at the top. 但是,每个新段落都出现在操作日志的最底部,当我希望它出现在顶部时。 Is there any way to put every new paragraph at the very top? 有什么办法可以将每个新段落放在最前面吗? Sorry if I'm missing something here, I've tried searching everything I could before asking about this myself. 抱歉,如果我在这里找不到任何东西,在尝试自己询问之前,我已尝试过搜索所有内容。 Thanks in advance! 提前致谢!

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

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