JavaScript 學習1

來源:互聯網
上載者:User

標籤:asc   比較   htm   使用者名稱   line   document   順序   href   通過   

JavaScript用於網頁和使用者之間的互動,比如提交的時候,進行使用者名稱是否為空白的判斷。 

javascript代碼必須放在script標籤中 

script標籤可以放在html的任何地方,一般建議放在head標籤裡

 

 樣本 1 : 通過javascript向文檔中輸出文本

< html >    < head >     < script >        document.write("這是 javascript");     </ script >    </ head > </ html >

document是javascript的內建對象,代表瀏覽器的文檔部分
document.write("Hello Javascript"); 向文檔寫入字串

<script>   document.write("第一段javascript");</script> <script>   document.write("第二段javascript");</script>

如果有多段script代碼,會按照從上到下,順序執行

 

樣本 2 : javascript和DOM結合的一個簡單例子

<button onclick="document.getElementById(‘text‘).style.display=‘none‘">隱藏文本</button> <button onclick="document.getElementById(‘text‘).style.display=‘block‘">顯示文本</button> <p id="text"> 這個是一段可以被javascript隱藏的文本</p>

這是一個javascript和DOM結合的一個簡單例子

onclick="..." 表示點擊button之後做的事情
document.getElementById 根據id擷取指定的元素
.style.display=‘none‘ 隱藏
.style.display=‘block‘ 顯示(準確的講,以block的形式顯示) 

 

 樣本 3 : 使用外部js檔案

和css一樣,當javascript代碼特別多,並且都寫在html裡的時候,會顯得比較繁雜,難以維護。 
這個時候可以採用和css一樣的手段,把javascript代碼剝離出來,單獨放在一個檔案裡,在html中引用該檔案即可。 
準備一個hello.js,裡面的是內容是 
document.write("hello javascript "); 
注意,不要寫 script標籤

<html>  <script src="路徑/檔案名稱.js"></script></html>

src 引入外部檔案

 

javascript有兩種注釋方式

單行注釋   用: //

多行註解  用     /*

       多行註解

       */

 

動手試試,才是王道。加油!!!

 

JavaScript 學習1

聯繫我們

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