首页 > 编程 > JavaScript > 正文

javascript 注释代码的几种方法总结

2019-11-19 18:09:51
字体:
来源:转载
供稿:网友

javascript注释代码一般有两种方法:

单行注释

多行注释

javascript单行注释

单行注释以"//"开头,到改行的末尾结束。下面是javascript单行注释实例:

<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多行注释

多行注释以 /* 开始,以 */ 结尾。下面的例子使用多行注释来解释代码:

<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

应用注释符号验证浏览器是否支持JavaScript脚本功能.

如果用户不能确定浏览器是否支持JavaScript脚本,那么可以应用HTML提供的注释符号进行验证。HTML注释符号是以“<--”开始以“-->”结束的。如果在此注释符号内编写JavaScript脚本,对于不支持JavaScript的浏览器,将会把编写的JavaScript脚本作为注释处理。

使用JavaScript脚本在页面中输出一个字符串,将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>

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表