2025-04-22

Html代碼 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>控制p屬性</title> 
<style> 
#outer{width:500px;margin:0 auto;padding:0;text-align:center;} 
#p1{width:100px;height:100px;background:black;margin:10px auto;display:block;} 
</style> 
<script> 
var changeStyle = function (elem, attr, value) 

    elem.style[attr] = value 
}; 
window.onload = function () 

    var oBtn = document.getElementsByTagName("input"); 
    var oDiv = document.getElementById("p1"); 
    var oAtt = ["width","height","background","display","display"]; 
    var oVal = ["200px","200px","red","none","block"]; 
 
    for (var i = 0; i < oBtn.length; i++) 
    { 
        oBtn[i].index = i; 
 
               
 
        oBtn[i].onclick = function () 
        { 
            this.index == oBtn.length – 1 && (oDiv.removeAttribute("style")); 
            changeStyle(oDiv, oAtt[this.index], oVal[this.index]) 
        }    
    } 
}; 
</script> 
</head> 
<body> 
<p id="outer"> 
<input type="button" value="變寬" /> 
<input type="button" value="變高" /> 
<input type="button" value="變色" /> 
<input type="button" value="隱藏" /> 
 
<input type="button" value="重置" /> 
<p id="p1"></p> 
</p> 
</body> 
</html> 

作者“Rebecca”
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *