JS——操作屬性

來源:互聯網
上載者:User

標籤: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——操作屬性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.