多个方法做图片交换:
- < html >
- < head >
- < title > Magic Hat </ title >
- < script language = "javascript" >
- function showRabbit1() {
- var hatImage = document .getElementById("topHat");
- var newImg = document .createElement("img");
- newImg.setAttribute("src","images/rabbit-hat.gif");
- var imgParent = hatImage .parentNode;
- //两步完成
- imgParent.insertBefore(newImg,hatImage);
- imgParent.removeChild(hatImage);
- var buttonEmement = document .getElementByTd("hocusPocus");
- button.setAttribute("value", "Get back in that hat!");
- }
- function showRabbit2() {
- var hatImage = document .getElementById("topHat");
- var newImg = document .createElement("img");
- newImg.setAttribute("src","images/rabbit-hat.gif");
- var imgParent = hatImage .parentNode;
- //一步完成,第一种
- imgParent.replaceChild(newImg,hatImage);
- var buttonEmement = document .getElementByTd("hocusPocus");
- button.setAttribute("value", "Get back in that hat!");
- }
- function showRabbit3() {
- var hatImage = document .getElementById("topHat");
- hatImage.setAttribute("src","images/rabbit-hat.gif");
- var buttonEmement = document .getElementById("hocusPocus");
- buttonEmement.setAttribute("value", "Get back in that hat!");
- buttonEmement.onclick = hiddenRabbit ;
- }
- function hiddenRabbit(){
- var hatImage = document .getElementById("topHat");
- hatImage.setAttribute("src","images/topHat.gif");
- var buttonEmement = document .getElementById("hocusPocus");
- buttonEmement.setAttribute("value", "Hocus Pocus!");
- buttonEmement.onclick = showRabbit3 ;
- }
- </ script >
- </ head >
- < body >
- < h1 align = "center" > Welcome to the DOM Magic Shop! </ h1 >
- < form name = "magic-hat" >
- < p align = "center" >
- < img src = "images/topHat.gif" width = "300" height = "225" id = "topHat" />
- < br />
- < br />
- < input type = "button" id = "hocusPocus" value = "Hocus Pocus!" onClick = "showRabbit3();" />
- </ p >
- </ form >
- </ body >
- </ html >