JavaScript函数

一、内置函数

1.alert()函数

alert()函数用于弹出一个消息对话框,该对话框包括一个“确定”按钮。
语法:alert(str);
参考案例:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>alert()函数</title>
</head>
<body>
<Script Language=JavaScript>
    function Clickme(){
	alert("请输入用户名");
	}	
</Script>
<p><a href=# onclick="Clickme()">单机试一下</a></p>
</body>
</html>

JavaScript函数

2.confirm()函数

confirm()函数用于显示一个请求对话框,包含一个“确定”按钮和一个“取消”按钮。
语法:confirm(str);
参考案例:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>confirm()函数</title>
</head>
<body>
<Script Language=JavaScript>
    function Checkme(){
	if(confirm("是否确定删除数据?")==true)
	      alert("成功删除数据");
		  else
		  alert("没有删除数据");
	}
</Script>
<p><a href=# onclick="Checkme()">删除数据</a></p>
</body>
</html>

JavaScript函数
JavaScript函数

3.prompt()函数

prompt()函数用于显示可提示用户输入的对话框,该对话包含一个“确定”按钮、一个“取消”按钮和一个文本框。
语法:prompt(text,defaultText);
参考案例:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>prompt()函数</title>
</head>
<body>
<Script Language=JavaScript>
    function Input(){
	var Myatr = prompt("请输入您的姓名");
	alert("您的姓名是:"+ Mystr)
	}	
</Script>
<p><a href=# onclick="Input()">录入姓名</a></p>
</body>
</html>

JavaScript函数
JavaScript函数

4.escape()函数

escape()函数用于对字符串进行编码,以便可以在所有的计算机上读取该字符串。
语法:escape(str);
参考案例:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>escape()函数</title>
</head>
<body>
<Script Language=JavaScript>
     document.write(escape("Hello world!")+"</br>");
	 document.write(escape("你好!?!=()#%&"));
</Script>
</body>
</html>

JavaScript函数

5.unescape()函数

unescape()函数可对通过escape()编码的字符串进行解码。
语法:unescape(str);
参考案例:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>unescape()函数</title>
</head>
<body>
<Script Language=JavaScript>
     str = escape ("Hello world!");
     document.write(escape(str)+"</br>")
	 document.write(unescape(str))
</Script>
</body>
</html>

JavaScript函数

6.eval()函数

eval()函数可以计算某个字符串,并执行其中的JavaScript代码。
语法:eval(str);
参考案例:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>eval()函数</title>
</head>
<body>
<Script Language=JavaScript>
     eval("x=1;y=2;document.write(x+y)");
     document.write("</br>")
	 document.write(eval(2*2))
</Script>
</body>
</html>

JavaScript函数

7.isNaN()函数

isNaN()函数用于测试参数是否不是一个数字。
语法:isNaN(x)
参考案例:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>isNaN()函数</title>
</head>
<body>
<Script Language=JavaScript>
	 document.write(isNaN(123));
	 document.write("</br>")
	  document.write(isNaN(-123));
	 document.write("</br>")
	  document.write(isNaN(1+2));
	 document.write("</br>")
	  document.write(isNaN(0));
	 document.write("</br>")
	  document.write(isNaN("Hello"));
	 document.write("</br>")
	  document.write(isNaN("2013/12/12"));
</Script>
</body>
</html>

JavaScript函数

8.parseFloat()函数

parseFloat()函数用于将字符串转换成浮点数字形式。
语法:parseFloat(str)
参考案例:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>parseFloat()函数</title>
</head>
<body>
<Script Language=JavaScript>
	 document.write(parseFloat("12.3")+1)
</Script>
</body>
</html>

JavaScript函数

9.parseInt()函数

parseInt()函数用于将字符串转换为整数数字形式。
语法:parseInt(str,radix)
参考案例:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>parseInt()函数</title>
</head>
<body>
<Script Language=JavaScript>
     str = parseInt("10");
	 document.write(str);
	 document.write("</br>")
	 str =  parseInt("f",16);
	 document.write(str);
	 document.write("</br>")
	 str = parseInt("010",2);
	 document.write(str);
	 document.write("</br>")
</Script>
</body>
</html>

JavaScript函数

二、自定义函数

function  函数名   (参数列表)
{
          函数体
}

谢谢大家浏览

JavaScript函数