標籤:disabled gre style 不可用 按鈕 i++ bsp margin back
操作屬性:
對象.setAttribute(‘屬性名稱‘,‘值‘); - 添加屬性
對象.getAttribute(‘屬性名稱‘); - 擷取屬性值,如無此屬性,那麼返回null
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css"> .div1 { width: 100px; height: 50px; float: left; margin-right: 10px; } </style></head><body> <div class="div1" dd="1"></div> <div class="div1" dd="1"></div> <div class="div1" dd="0"></div> <div class="div1" dd="0"></div> <div class="div1"></div> <div class="div1"></div></body></html><script type="text/javascript"> var aa = document.getElementsByClassName("div1"); for (var i = 0; i < aa.length; i++) { if (aa[i].getAttribute("dd") == "1") aa[i].style.backgroundColor = "green"; else if (aa[i].getAttribute("dd") == "0") aa[i].style.backgroundColor = "yellow"; else aa[i].style.backgroundColor = "red"; }</script>
對象.removeAttribute(‘屬性名稱‘); - 移除屬性
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title></head><body> <input type="button" value="按鈕111" id="dd1" /> <input type="button" value="按鈕222" id="dd2" /></body></html><script type="text/javascript"> var aaa = document.getElementById("dd1"); var bbb = document.getElementById("dd2"); //按鈕111的點擊事件 aaa.onclick = function () { //按鈕111添加一個屬性不可用 this.setAttribute("disabled", "disabled"); //擷取aaa中屬性value的值 var ccc = this.getAttribute("value"); alert(ccc); } //bbb的點擊事件 bbb.onclick = function () { //移除aaa的disabled屬性 aaa.removeAttribute("disabled"); }</script>
2.驗證5+5=?
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title></head><body> 5+5= <input type="text" id="dd1" data="10" /> <input type="button" id="dd2" value="驗證"/></body></html><script type="text/javascript"> var aaa = document.getElementById("dd1"); var bbb = document.getElementById("dd2"); //按鈕111的點擊事件 //bbb的點擊事件 bbb.onclick = function () { var txt = aaa.getAttribute("data"); if (txt == aaa.value) alert("正確"); else alert("笨蛋"); }</script>
定時器:
window.setTimeout(function(){},間隔時間毫秒);
- 定時炸彈,順延強制,只執行一次
window.setInterval(function(){},間隔的時間毫秒);
- 無限迴圈,每一次迴圈有間隔時間,一般不要小於20毫秒
- 它是有傳回值的,可以用一個變數來接收這個定時器對象
window.clearInterval(要關閉的定時器對象);
一旦執行這句代碼,會立刻停止此定時器對象的執行
對象.offsetWidth
JS——操作屬性