建立一個節點
window.onload=function(){
var demo=document.getElementById("demo");
//獲取第一個按鈕索引爲0
var btn=document.getElementsByTagName("button")[0];
//按鈕鼠標事件
btn.onclick=function(){
//建立一個li
var newdiv=document.createElement("li");
//插入一個li
demo.appendChild(newdiv);
//克隆而且插入一個demo
demo.appendChild(demo.cloneNode(demo));
}
}
插入到某個元素的前面
insertBefore(插入的新節點,參照節點[0]) 插入節點 :
inserBefore(test,null) 若是第二個參數爲bull 則默認生成的盒子放到最後面!
移除子節點
removeChild(被移除的子節點!)
克隆一個對象:
克隆對象.appendChild( 克隆對象.cloneNode(克隆的對象))
cursor:pointer:設置當前對象可點擊!
得到父節點下面的一個子節點
節點=父節點.children
獲取節點的屬性
getAttribute("屬性標籤")
設置節點的屬性:
setAttribute("屬性",「值")
例如咱們要把一個類名改成demo
div.setAttribute("class","demo")
刪除某個屬性:
removeAttribute("屬性標籤)
內置對象:(也就是類)
聲明日期:
var date=new Date();
例子:
var deta=new Date();
alert(deta.getTime());
alert(deta.valueOf());
從1970年1月1日 到如今的時間
定時器 :
setInterval(fun,1000); 每隔一秒鐘運行一次 fun 函數
時間換算案列
window.onload=function(){
var box=document.getElementById("box");
var endtime=new Date("2017/12/12 17:30:00");
setInterval(clock,1000);
function clock(){
var nowTime=new Date();
// console.log(nowTime.getTime());
var second=parseInt((endtime.getTime()-nowTime.getTime())/1000);
//一小時等於3600秒
var d=parseInt(second/ 3600 / 24); //得到天數
// console.log(d);
var h=parseInt(second/ 3600 % 24); //得到小時
// console.log(h);
var m=parseInt(second/60 % 60); //分鐘
// console.log(m)
var s=parseInt(second%60); //秒
d<10 ? "0"+d : d;
h<10 ? "0"+h : h;
m<10 ? "0"+m : m;
s<10 ? "0"+s : s;
box.innerHTML="距離毀滅時間還剩:"+d+"天 "+h+"小時 "+m+"分鐘 "+s+"秒";
}
}