javaScript筆記01

來源:互聯網
上載者:User

標籤:擷取   執行   javascrip   java   head   nbsp   his   順序   href   

  所謂勇氣就是明知前方的路途充滿了荊棘險阻,但還是義無反顧的的走下去。

  1 Javasrcipt定義的三種方式

   ·1 head中的script指令碼

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>js-01</title>    <script type="text/javascript">        alert(‘hello World!‘);    </script></head><body>    你好,body!</body></html>

執行順序是先會彈出“hello World!”,然後介面顯示‘你好,body!’

   ·2 超連結中直接定義

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>js-02</title></head><body>    <a href="javascript:alert(0)">我是js</a></body></html>

這個點擊超連結就會執行彈出框;

   ·3 外部引入js的方式

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>js-03</title>    <script type="text/javascript" src="index.js"></script></head><body>    <div id="mydiv">this is div</div></body></html>

index.js檔案很簡單:

alert("你好,index.js");

但是此時會存在問題的,如果js對div標籤存在一定的操作,那麼會出現問題:

修改index.js檔案:

alert("你好,index.js");// 擷取div的idvar id=document.getElementById("mydiv");alert(id);

此時由於先執行Javascript指令碼,但是js擷取div的id時還沒有執行到div的標籤,那麼擷取的div的id就是null.此時我們可以將script定義在div的下面,或者使用window.onload()函數來解決,此函數的功能是等待頁面載入完畢之後才執行js指令碼:

// 等待頁面載入完畢執行js指令碼window.onload=function(){    alert("你好,index.js");    // 擷取div的id    var id=document.getElementById("mydiv");    alert(id);}

 

javaScript筆記01

相關文章

聯繫我們

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