Javascript入門(二)變數、擷取元素、操作元素

來源:互聯網
上載者:User

標籤:ntb   src   color   text   def   asc   通過   font   got   

一、變數

       Javascript 有五種基礎資料型別 (Elementary Data Type) number、String、boolean、undefined、null 一種複合類型:object

 

二、使用getElementById方法擷取元素

      方式一:

     這裡的元素,指html裡的標籤,通過內建docuement的 ‘getElementById‘ 方法擷取頁面上設定了 id 屬性的元素, 擷取一個html對象,並為其賦值,

     先不賦值看個例子:

<!DOCTYPE html><html lang="en"><head>     <script type="text/javascript">        var div1  = document.getElementById(‘div1‘);     </script></head><body>    <div id = ‘div1‘ title="This is a label"> This is label </div></body></html>

     然後開啟瀏覽器,吧滑鼠點在該標籤(div1)上面:會有一個提示出來,這個就是div1 這元素的title 儲存的資訊

     

    接下去通過擷取來修改title 這個標籤,但是如下方法會失敗,因為頁面掃描方式從上到下,先掃描不存在的變數就沒法改變了,會報錯。

<!DOCTYPE html><html lang="en"><head>     <script type="text/javascript">        var div1  = document.getElementById(‘div1‘).title = ‘I got it!‘;     </script></head><body>    <div id = ‘div1‘> This is label </div></body></html>

    所以,要把script放在div標籤下面

<!DOCTYPE html><html lang="en"><head>     <script type="text/javascript">             </script></head><body>    <div id = ‘div1‘> This is label </div>    <script type="text/javascript">          document.getElementById(‘div1‘).title = ‘I got it!‘;     </script></body></html>

     修改title後的效果

     

     如果在 <head>標籤裡的<script>中改的話,可以用window.onload方法, 可以在頁面載入完再修改屬性,這樣就能掃描到了。

<!DOCTYPE html><html lang="en"><head>     <script type="text/javascript">         window.onload = function(){        document.getElementById(‘div1‘).title= ‘I got it!‘;       }    </script></head><body>    <div id = ‘div1‘> This is label </div> </body></html>

 

三、操作元素

      JS可以通過擷取元素,對元素屬性修改;

      ---   操作的方法:   1、點的操作 : ‘.’  ;

                                   2、括弧操作 :  ‘[ ]’ ;

      ---   屬性修改方式:1、JS 的屬性寫法和html一樣;

      例子1:修改屬性:

<!DOCTYPE html><html lang="en"><head>        window.onload = function(){        var input  = document.getElementById(‘input1‘);        var target = document.getElementById(‘text1‘);        //attribute         var val  = input.value;         var type = input.type;         var name = input.name;         //change attribute        target.style.color = ‘red‘;        target.style.fontSize = ‘val‘;     }    </script></head><body>     <input type = "text" name = "setSize" id="input1" value = "20px">     <h1 id = "text1"> SetSize </h1> </body></html>

     效果,下面的setSize

     

      

  

 

     

    

Javascript入門(二)變數、擷取元素、操作元素

相關文章

聯繫我們

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