JavaScript
Hello.html
示例2-JS位于将脚本编写为函数 head
<html> 避免页面载入时执行该脚本 <head> <script> function disp_alert() { alert("Hello World!"); } </script> 当函数被调用时 </head> JS代码才会被执行 <body> <input type="button" onclick="disp_alert()" value="OK" /> </body> </html>
eval.html
【返回】
isNaN函数
功能:用于检查其参数是否是非数字值。
用法:ቤተ መጻሕፍቲ ባይዱsNaN(x)
<script> document.write(isNaN(123)+ "<br />") document.write(isNaN(-1.23)+ "<br />") document.write(isNaN(5-2)+ "<br />") document.write(isNaN(0)+ "<br />") document.write(isNaN("Hello")+ "<br />") document.write(isNaN("2012/12/21")+ "<br />") </script> isnan.html false false false false true true
函数示例1
<html> <head> 参数不指定类型 <script> function fun1(txt) { alert(txt); } </script> </head> 函数在通常在<head>部分定义。 <body> <form> <input type="button" onclick="fun1('hello')" value="调用函数"> </form> </body> </html>
parseint.html
被转换为数字,则返回NaN
parseFloat 函数 功能:解析一个字符串,并返回一
用法:parseFloat(string)
<script> document.write(parseFloat("10") + "<br />") document.write(parseFloat("10.00") + "<br />") document.write(parseFloat("10.33") + "<br />") document.write(parseFloat("34 45 66") + "<br />") document.write(parseFloat(" 60 ") + "<br />") document.write(parseFloat("40 years") + "<br />") document.write(parseFloat("He was 40") + "<br />") </script> parsefloat.html
用法:unescape(string)
<script> var str="you are welcome!"; str=escape(str); document.write(str+"<br />"); document.write(unescape(str)); </script> you%20are%20welcome%21 you are welcome!
head.html
(一般放在head中)
示例3-JS位于外部文件
<html> 引用外部 js 文件 <head> 外部js文件不能包含 <script> 标签 <script src="js/1.js"> </script> 1.js: function disp_alert() </head> { alert("Hello World!"); } <body> <input type="button" onclick="disp_alert()" value="OK" /> </body> </html> 【返回】
JavaScript内置常规函数
9个常规函数:
消息 框
alert函数:显示一个警告对话框,包括一个OK按钮。 confirm函数:显示一个确认对话框,包括OK、Cancel按钮。 prompt函数:显示一个提示输入对话框,提示等待用户输入。 parseInt函数:将字符串转换成整数数字形式(可指定几进制)。 √ parseFloat函数:将字符串转换成符点数字形式。√
JavaScript可以出现在HTML的任意地方(详见示例)
示例1-JS位于body
当页面载入时,会执行位于 <html> body部分的脚本 <body> <script> document.write("<h1>Hello World!</h1>") </script> </body> </html>
eval函数:可计算某个字符串,并执行其中的的JS代码的结果。 isNaN函数:测试是(true)否(false)不是一个数字。 escape函数:将字符转换成Unicode码。 unescape函数:对escape函数编码的字符进行解码。 【返回】
JavaScript消息框
警告框 确认框
提示 输入框
函数示例2
<html> <head> <script> function add(a,b) 即使有返回值也不能指定函数类型 { return a+b; } </script> </head> <body> 6+5=<script>document.write(add(6,5))</script> </body> 当页面载入时,会执行位于 body部分的JavaScript </html>
prompt提示输入框示例
<html> <head> <script> function disp_prompt() { var name=prompt("请输入您的名字","wustzz"); if (name!=null && name!="") { document.write(name +"你好!" ); } 默认值 } 如点击【确认】,那么返回值为输入的值 </script> 如点击【取消】,那么返回值为 null </head> <body> <input type="button" onclick="disp_prompt()" value="显示提示框" /> </body> </html>
alert警告框示例
<html> <head> <script> function disp_alert() { alert("我是"+'\n'+"警告框!"); } </script> </head> <body> <input type="button" onclick="disp_alert()" value="显示警告 框" /> </body> </html>
alert.html
confirm确认框示例
<html> <head> <script> function disp_confirm() { var r=confirm("按下按钮"); if (r==true) { document.write("您按了确认!") ; } else { document.write("您按了取消!"); } } </script> 如点击【确认】,则返回值为 true </head> 如点击【取消】,返回值为 false <body> <input type="button" onclick="disp_confirm()" value="显示确认框" /> </body> </html> confirm.html