js的DOM編程基礎

來源:互聯網
上載者:User

標籤:dom基礎   事件   搜尋方塊   

1、DOM編程

  是檔案物件模型,是可擴充的程式設計語言的介面,是專門為修改標籤服務的;

  思路:先找要修改的標籤,對其進行增加/修改,可以修改某一個屬性/樣式,從而讓標籤動起來;

  DOM是和js結合來使頁面動起來的;


2、DOM選取器

(1)、找標籤的:(document是一個對象)

  document.getElementById(‘id‘);    //id是唯一的

  document.getElementsByName(‘name‘);   //名字可以相同

  document.getElementsByTagName(‘tagname‘);  //通過標籤尋找(a、p、div、)

(2)、建立標籤

  document.createElement(‘a‘);

(3)、擷取/修改樣式

  obj.className

(4)、擷取/設定屬性

  setattribute(key, val);   getattribute(key); 

(5)、擷取/修改樣式中的屬性

  obj.style.屬性

注意:js中的屬性名稱可能和CSS中的名稱不一致:

以上方法的樣本如下:

<!DOCTYPE html><html>    <head>        <meta http-equiv="content-type" content="text/html;charset=utf-8">        <title>js1</title>        <style>            .show{                            }            .hide{                display:none            }        </style>    </head>        <body>        <div id = ‘t1‘>abc</div>        <div id = ‘t2‘>d<span>e</span>f</div>        <hr/>                        <!--        <script type=‘text/javascript‘>                var id1 = document.getElementById(‘t1‘); //文檔對象            console.log(id1.innerText);  //獲得標籤內容            id1.innerText = ‘123‘;  //對標籤內容進行修改            console.log(id1.innerText);            var id2 = document.getElementById(‘t2‘); //文檔對象            console.log(id2.innerHTML); //innerText和innerHTML等價        </script>--><!--            <div id = ‘123‘>abcdefghijkl</div>            <hr/>            <div name = ‘t1‘>jkl</div>            <hr/>            <div name = ‘t1‘>uio</div>            <hr/>            <script type = ‘text/javascript‘>                var s = document.getElementById(‘123‘);                s.innerText = ‘hao‘;                var names = document.getElementsByName(‘t1‘);                //names[0].innerText = ‘de‘;                //names[1].innerText = ‘hen‘;                for(var items in names){                    names[items].innerText = ‘zaima?‘;                }                var all = document.getElementsByTagName(‘div‘);                for(var items in all){                all[items].innerText = ‘xing‘;            }--><!--        <div id = ‘t1‘ class = ‘show‘ name = ‘alex‘>內容</div>            <div id = ‘t2‘ style = ‘width:500px;height:200px;border:2px solid #333;‘></div>-->                <!--        <form id = ‘F1’ action = ‘https://www.sogou.com/web?‘ method = ‘GET‘>            <input type = ‘text‘ name = ‘query‘/>               <input type = ‘submit‘ value = ‘提交‘ />            <input type = ‘button‘ value = ‘偽提交‘ onclick = ‘Foo();‘ />                //點擊執行Foo()函數,是一個事件        </form>                <script type = ‘text/javascript‘>        /*建立標籤方法1、            var tag = document.createElement(‘a‘);            tag.href = ‘http://www.baidu.com.cn‘            tag.innerText = ‘點我啊‘;            var id1 = document.getElementById(‘t1‘);  //尋找父容器            id1.appendChild(tag);        */        /*        建立標籤方法2、        var tag = "<a href = ‘http://www.baidu.com.cn‘>走你</a>";        var id1 = document.getElementById(‘t1‘);        id1.innerHTML = tag;        */                    /*        var id1 = document.getElementById(‘t1‘);        id1.className = ‘hide‘;   //修改className        */        /*        var id1 = document.getElementById(‘t1‘);        console.log(id1.getAttribute(‘name‘)); //擷取屬性        id1.setAttribute(‘name‘, ‘oldboy‘);        console.log(id1.getAttribute(‘name‘));                var id1 = document.getElementById(‘t2‘);        console.log(id1.style.width);        */        提交表單        function Foo(){            var id1 = document.getElementById(‘F1‘);            id1.submit();        }        </script>  -->    </body></html>

3、常用事件

當設定事件時,相應的執行操作會執行其所對應的函數。

  onclick:點擊事件

  onfocus:元素獲得焦點

  onblur:元素失去焦點

一個應用(搜尋方塊):

<!DOCTYPE html><html>    <head>        <meta http-equiv="content-type" content="text/html;charset=utf-8">        <title>頁面一</title>        <style>            .gray{                color:gray;            }            .black{                color:black;            }        </style>    </head>        <body>        <input type = ‘text‘ class = ‘gray‘ id = ‘tip‘ value = ‘請輸入關鍵字‘         onfocus = ‘Enter();‘ onblur = ‘Leave();‘/>                <script type = ‘text/javascript‘>            function Enter(){                var id = document.getElementById(‘tip‘);                id.className = ‘black‘;                                if(id.value == ‘請輸入關鍵字‘ || id.value.trim() == ‘‘){                    id.value = ‘‘;                }            }            function Leave(){                var id = document.getElementById(‘tip‘);                var val = id.value;                                if(val.length == 0 || id.value.trim() == ‘‘){                    id.className = ‘gray‘;                    id.value = ‘請輸入關鍵字‘;                }else{                    id.className = ‘black‘;                }            }        </script>    </body></html>

運行結果

650) this.width=650;" src="http://s5.51cto.com/wyfs02/M01/89/42/wKioL1gN_MXgi6tzAAACl-ntYp0774.png-wh_500x0-wm_3-wmp_4-s_3624450049.png" title="QQ20161024202112.png" alt="wKioL1gN_MXgi6tzAAACl-ntYp0774.png-wh_50" />

650) this.width=650;" src="http://s2.51cto.com/wyfs02/M02/89/42/wKioL1gN_OWAACzWAAACBhEKNQk238.png-wh_500x0-wm_3-wmp_4-s_2385798159.png" title="QQ20161024202143.png" alt="wKioL1gN_OWAACzWAAACBhEKNQk238.png-wh_50" />

撤走之後,沒有內容又會恢複原樣!!!



本文出自 “11586096” 部落格,請務必保留此出處http://11596096.blog.51cto.com/11586096/1865176

js的DOM編程基礎

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.