1. 程式人生 > >JavaScript發帖案例

JavaScript發帖案例

window.onload=function(){ //頁面全部載入完成後,執行 var obtn=document.getElementById("btn");//我要發帖的按鈕 var opost=document.getElementById("post");//輸入面板 var osubmit=document.getElementById("submit");//釋出的按鈕 var ocontent=document.getElementById("content");//輸出列表內容 var tid;//定時器ID var flag=false;//用於標記滑鼠是否放在輸入面板上 //滑鼠放到按鈕上時 obtn.onmouseover=function
(){
opost.style.display="block"; } //滑鼠離開按鈕之後 obtn.onmouseout=function(){ timeDelay(); } //滑鼠放到輸入面板上 opost.onmouseover=function(){ flag=true; timeDelay(); } //滑鼠離開輸入面板後 opost.onmouseout=function(){ timeDelay(); } //有使用鍵盤的情況下 document.onkeydown=function(event){ flag=true; timeDelay(); } //時間延遲的方法 function timeDelay
(){
if(flag==false){ tid=setTimeout(function(){ opost.style.display="none"; },5000);//時間延遲5秒 } if(flag==true){ opost.style.display="block"; if(tid != "undefined"){ clearTimeout(tid); } } } var date=new Date();//獲取系統時間 //年 var year=date.getFullYear(); //月
var month=date.getMonth()+1; //日 var day=date.getDate(); //時 var hour=date.getHours(); //分 var minute=date.getMinutes(); //秒 var second=date.getSeconds(); //時間拼接 var time=year+"-"+addzero(month)+"-"+addzero(day)+" "+addzero(hour)+":"+addzero(minute)+":"+addzero(second) //時間補0函式 function addzero(num){ if(num<10){ return "0"+num; }else{ return num; } } //點擊發布,提交按鈕 osubmit.onclick=function(){ var tou=new Array("tou01.jpg","tou02.jpg","tou03.jpg","tou04.jpg"); flag=false; opost.style.display="none"; ocontent.style.display="block"; //1.建立li節點 var li=document.createElement("li"); //2.建立img節點 var img=document.createElement("img"); var r=parseInt(Math.random()*tou.length);//隨機獲取圖片下標 img.src="images/"+tou[r]; li.appendChild(img); //將img追加到li末尾 //3.建立h1節點 var h1=document.createElement("h1"); var title=document.getElementById("title").value;//獲取標題內容 var bankuai=document.getElementsByTagName("select"); h1.innerHTML=title; li.appendChild(h1); //將h1追加到li末尾 //4.建立p節點 var p=document.createElement("p"); p.innerHTML="版塊:"+bankuai[0].value+" 發表時間:"+time; p.style.fontSize="12px"; p.style.color="#999"; li.appendChild(p); //將p追加到li末尾 li.style.borderBottom="1px dashed #dcdcdc"; //5.將建立好的li節點插入div#content下的ul中第一位 ocontent.firstElementChild.insertBefore(li,ocontent.firstElementChild.firstChild); }//點擊發布結束 }//window.onload結束