2014-09-30 40 views
0

需要創建一個消息日誌,其中消息與時間一起保存。創建一個日誌並附加

我有這樣的代碼,但acresentar一個新的消息被刪除舊的並由新的替換。

var textons = prompt("Digite sua mensagem para a ONS", ""); 
    var areaons = prompt("Digite a area onde o problema ocorreu", ""); 



    var date = new Date(); 
    var d = date.getDate(); 
    var day = (d < 10) ? '0' + d : d; 
    var mes = date.getMonth() + 1; 
    var month = (mes < 10) ? '0' + mes : mes; 
    var yy = date.getYear(); 
    var year = (yy > 100) ? yy - 100 : yy; 
    var hours = date.getHours(); 
    var min = date.getMinutes(); 
    var minutes = (min < 10) ? '0' + min : min; 
    var sec = date.getSeconds(); 
    var seconds = (sec < 10) ? '0' + sec : sec;  


    if (areaons != null && textons != null) { 
     document.getElementById("logdescricao").innerHTML = 
     "ONS: " + textons; 
     document.getElementById("logarea").innerHTML = 
     areaons; 

     document.getElementById("loghoracos").innerHTML = 
     day + "/" + month + "/" + year + " " + hours + ":" + minutes + ":" + seconds; 

     document.getElementById("loghoralocal").innerHTML = 
     day + "/" + month + "/" + year + " " + hours + ":" + minutes + ":" + seconds; 
    } 
} 

我想過使用.append(函數),但沒有成功。

我該如何讓文本停止被重寫並通過保存?

感謝

回答

0

如果我理解正確的話,你只是希望它添加到每個部分覆蓋代替? 然後,所有你需要做的是「+ =」,而不是「=」

document.getElementById("logdescricao").innerHTML += "ONS: " + textons 

然而,這可能不是這樣做的最佳方式。

你可以嘗試:

var logdescricaoText = document.createTextNode("ONS: "+textons); 
document.getElementById("logdescricao").appendChild(logdescricaoText); 

不過,說真的,如果你想的事情列出來,那麼你應該使用一個表。這將允許更加整潔和結構化的造型。 在你的HTML:

<table> 
    <thead> 
     <tr> 
      <td>Descricao</td> 
      <td>LogMessage</td> 
      <!-- other field headers like 'time' --> 
     </tr> 
    </thead> 
    <tbody id=logs> 
    </tbody> 
</table> 

然後你可以使用JavaScript(你的 '如果' 塊中,而不是所有的 「.innerHTML =」 的東西)

var logLine = document.createElement("tr"); 
document.getElementById("logs").appendChild(logLine); 

var descricao = document.createElement("td"); 
descricao.appendChild(document.createTextNode("ONS: "+textons)); 
logLine.appendChild(descricao); 

var logArea = document.createElement("td"); 
logArea.appendChild(document.createTextNode(areaons); 
logLine.appendChild(logArea); 
etc.. 

此外,你可能想例如,將CSS樣式應用於HTML的「thead」部分。在你的CSS文件中:

thead { 
    font-weight: bold; 
}