大神們 錯在哪里?????
<form>
? <!--當點擊相應按鈕,執行相應操作,為按鈕添加相應事件-->
? ? <input type="button" value="改變顏色" onclick="yanse()"> ?
? ? <input type="button" value="改變寬高" onclick="kuangao()">
? ? <input type="button" value="隱藏內容" onclick="yincang()">
? ? <input type="button" value="顯示內容" onclick="xianshi()">
? ? <input type="button" value="取消設置" onclick="chongzhi()">
? </form>
? <script type="text/javascript">
? ? var mydiv = document.getElementById("txt");
//定義"改變顏色"的函數
? ? function yanse(){
? ? ? ? mydiv.style.color="red";
? ? ? ? mydiv.style.backgroundColor="#ccc";
? ? }
//定義"改變寬高"的函數
? ? function kuangao(){
? ? ? ?mydiv.style.width="200px";
? ? mydiv.style.height="300px";
? ? }
//定義"隱藏內容"的函數
? ? function yincang(){
? ? ? ?mydiv.style.display="none";
? ? }
//定義"顯示內容"的函數
? ? function xianshi(){
? ? ? ? mydiv.style.display="block";
? ? }
//定義"取消設置"的函數
? ? function chongzhi(){
? ? ? ? ?var mychose = confirm();
? ? if(mychose==true){
? ? ? ?mydiv.removeAttribute("style");
? ? ? ? }
? ? }
2019-08-05
用var定義的變量為局部變量,不用var修飾才為全局變量;var mydiv = document.getElementById("txt");你把這里的var去掉試試; 但是良好的代碼風格里面,你最好加上var,也就是再函數里面定義變量,這樣以后往回看就不會很迷糊了
2019-08-04
除了 confirm()有用,其余點擊都沒反應