//得到HTML中的body節點 var body = document.getElementsByTagName("body")[0]; //將div添加到body節點下 body.appendChild(divShow); body.appendChild(p);//把p添加到body下
//為元素添加單擊事件 //節點對象.事件名 = new function(){};
//得到所有的<a>標簽 var alist = document.getElementById("div").getElementsByTagName("a");
for(var i = 0;i < alist.length; i++){ //當鼠標點擊時切換圖片 alist[i].onclick = function(){ //this就表示當前被點擊的節點 //點誰獲得誰的href和title的值 var href = this.getAttribute("href"); var img = this.getElementsByTagName("img")[0]; var title = img.getAttribute("title");
//修改img標簽的src屬性 var img = document.getElementById("img"); img.setAttribute("src",href);
//修改p標簽的文本 var p = document.getElementById("p"); p.firstChild.nodeValue=title;
//取消<a>標簽的跳轉 return false; }
//當鼠標稱上去的時候切換圖片 alist[i].onmousemove = function(){ //this就表示當前被點擊的節點 //點誰獲得誰的href和title的值 var href = this.getAttribute("href"); var img = this.getElementsByTagName("img")[0]; var title = img.getAttribute("title");
//修改img標簽的src屬性 var img = document.getElementById("img"); img.setAttribute("src",href);
//修改p標簽的文本 var p = document.getElementById("p"); p.firstChild.nodeValue=title;