在我的客户端聊天应用程序jsp页面中,我有一个文本区域,显示来自代理端的消息,并显示来自客户的消息。
工作正常。我的javaScript代码是,
function sendMessage(){
message =trim( document.getElementById("message").value);
document.getElementById("message").value = "";
if(message != null && message !="null" && message !=""){
try
{
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var checkMsg = trim(xmlhttp.responseText.toString());
textarea = document.getElementById('textarea');
if(checkMsg != "null" && checkMsg != null && trim(checkMsg).length != 0) {
if(trim(textarea.value) == ""){
textarea.value = message = checkMsg;
textarea.scrollTop = textarea.scrollHeight;
}
else{
textarea.value += "\n"+checkMsg;
textarea.scrollTop = textarea.scrollHeight;
}
}
}
};
xmlhttp.open("POST", "SendMessageAction",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("charset","UTF-8");
xmlhttp.send("sessionId="+encodeURIComponent(sessionId)+"&userId="+encodeURIComponent(userId)+"&securekey="+encodeURIComponent(secureKey)+"&message="+encodeURIComponent(message)+"&sid="+Math.random());
}
catch(err)
{
alert(err.description);
}
}
}
因为我需要将当前时间包含在文本区域右侧角落的文本区域中附加的每条消息中。
我可以用消息附加当前时间,但我需要在textarea的右侧角落附上每条消息吗?
我该怎么做。请提供一些想法或一些代码。
答案 0 :(得分:0)
这是一个显示当前日期/时间的函数:
如果需要,您只能提取时间线。
function update(){
time = new Date();
year = (time.getFullYear());
month = (time.getMonth() + 1);
date = time.getDate();
hours = time.getHours();
mins = time.getMinutes();
secs = time.getSeconds();
if(mins.length<2){mins = "0"+secs}
if(secs.length<2){secs = "0"+secs}
if(month.length<2){month = "0"+secs}
if(date.length<2){date = "0"+secs}
// Config Starts
if(hours>12){hours=hours-12}else{hours}
format = year+"-"+month+"-"+date+" @ "+hours+":"+mins+":"+secs;
document.form1.box1.value = format;
// Config Ends
}
setInterval("update()",1000);
要显示结果,请使用:
<script>
#right
{
float: right;
}
</script>
<div id="right">
<p>Current Date/Time is: <INPUT name="box1" type="text" size="20"></p>
</div>