為了程序/代碼的易讀性,基本上每一種編程語言都有注釋的功能,javascript也不例外,javascript注釋代碼有多種形式,本文章向大家介紹javascript注釋代碼的兩種方法,
javascript注釋代碼一般有兩種方法:
	單行注釋
	多行注釋 
javascript單行注釋
單行注釋以"//"開頭,到改行的末尾結(jié)束。下面是javascript單行注釋實(shí)例:
<html><head><title>javascript單行注釋</title><script language="javascript"><!--// The first alert is belowalert("An alert triggered by JavaScript!");// Here is the second alertalert("A second message appears!");// --></script></head><body></body></html>javascript多行注釋
多行注釋以 /* 開始,以 */ 結(jié)尾。下面的例子使用多行注釋來解釋代碼:
<html><head><title>javascript多行注釋</title><script language="javascript"><!--/*Below two alert() methods are used tofire up two message boxes - note how thesecond one fires after the OK button on thefirst has been clicked*/alert("An alert triggered by JavaScript!");alert("A second message appears!");// --></script></head><body></body></html>如何解決瀏覽器不支持javascript
應(yīng)用注釋符號(hào)驗(yàn)證瀏覽器是否支持JavaScript腳本功能.
如果用戶不能確定瀏覽器是否支持JavaScript腳本,那么可以應(yīng)用HTML提供的注釋符號(hào)進(jìn)行驗(yàn)證。HTML注釋符號(hào)是以“<--”開始以“-->”結(jié)束的。如果在此注釋符號(hào)內(nèi)編寫JavaScript腳本,對(duì)于不支持JavaScript的瀏覽器,將會(huì)把編寫的JavaScript腳本作為注釋處理。
使用JavaScript腳本在頁面中輸出一個(gè)字符串,將JavaScript腳本編寫在HTML注釋中,如果瀏覽器支持JavaScript將輸出此字符串,如果不支持將不輸出此字符串,代碼如下:
<html><head><title>Hide scripts using comments.</title><script language="javascript" type="text/javascript"><!--document.write("您的瀏覽器支持JavaScript腳本!");//--></script></head><body>Page content here...</body></html>通過此文希望能幫助到有需要的朋友,謝謝大家對(duì)本站的支持!
新聞熱點(diǎn)
疑難解答