jsは伝言板を作っています.(コメント削除のコメントができる時間) 4147 ワード javascript Document <div class="btn"> <input type="button" value=" " id="addBtn"/> </div> </div> </div> <script src="index.js"/> </code></pre> </div> <p> </p> <div class="cnblogs_Highlighter"> <pre><code>let data = [{ username:" 0", time:"2017-09-07 09:11:00", text:" 0"},{ username:" 1", time:"2017-09-08 09:11:00", text:" 0"},{ username:" 2", time:"2017-09-09 09:11:00", text:" 0" }]; render(); addBtn.addEventListener("click",() =>{ addMessage(); }) content.addEventListener("keydown",(e) =>{ if (e.keyCode === 13){ addMessage(); } },false) exhibit.addEventListener("click" , (e) =>{ console.log(e) if (e.target.className === "removeBtn"){ // e.preventDefault(); e.preventDefault(); let i =e.target.getAttribute("_id"); data.splice(i,1); render(); } }, false) function addMessage(){ data.push({ username:` ${data.length}`, time:getNowTime(), text:content.value }); content.value = ""; render(); } function render(){ exhibit.innerHTML = data.map((item, index) =>{ return `<div class="message"> <div class="info"> <span>${item.username}</span> <span>${item.time}</span> </div> <p>${item.text}</p> <div class="btn"> <a href="#" class="removeBtn" _id="${index}"> </a> </div> </div>` }).join(""); } function getNowTime(){ let date = new Date(); let year = date.getFullYear(); let month = date.getMonth() + 1; month =judgeTime(month); let day = date.getDate(); day = judgeTime(day); let hours = date.getHours(); hours = judgeTime(hours); let minutes = date.getMinutes(); minutes = judgeTime(minutes); let seconds = date.getSeconds(); seconds = judgeTime(seconds); let nowTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; return nowTime; } function judgeTime(time){ if(time < 10 && time > 0){ time = "0" + time; } return time; } </code></pre> </div> <p> </p> </div> <p> :https://www.cnblogs.com/gao2/p/11528682.html</p> </div> </div> </div> </div> JavaScriptの4つの数値の整理方法