JS:1.3,函数(function)

ylbtech-JS:函数-导航
JS:3.1,函数(function)-定义 返回顶部

1,定义函数语法

通过定义函数名称,参数和代码语句来创建函数。

function 函数名([参数1,][参数2,][...])
{
	语句:
}

备注:
[]内的内容可以不写。

参数是函数中使用的变量,变量的值是别调用函数按值传值的。通过将函数放置在文档的头部分(head),函数中的代码将在函数被调用之前加载。

2,怎样调用函数

一个函数在没被调用之前将不会执行。
(1)调用包含参数的函数。

函数名([参数1,][参数2,][...])

(2)调用不包含参数的函数。

函数名()
JS:3.2,函数-返回函数 返回顶部
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
function total(a,b)
{
    result=a+b;
    return result;
}
</script>
</head>

<body>
<h1>1,返回语句</h1>
<script language="javascript">
var a=3;
var b=1;
var sum=total(a,b);
document.write(sum);
</script>
</body>
JS:3.3,函数-调用一个函数 返回顶部
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
function fun()
{
    alert("Hello!");
}
</script>
</head>

<body>
<pre>
<h1>2,调用一个函数</h1>
<input type="button" onclick="fun()" value="调用函数" />
<a href="javascript:fun()">调用函数</a><br />
<p>
通过按下按钮,一个函数将被调用。这个函数将弹出一个消息框
</p>
</pre>
</body>
</html>
JS:3.4,函数-调用一个函数(带参数) 返回顶部
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
function fun(word)
{
    alert("Hello "+word+"!");
}
</script>
</head>

<body>
<pre>
<h1>2,调用一个函数(带参数)</h1>

<input type="button" onclick="fun('小姐')" value="调用函数" />
<p>
通过按下按钮,一个函数将被调用。这个函数将弹出一个消息框。
</p>
</pre>
</body>
</html>
JS:3.5,函数-返回值的函数 返回顶部
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
function fun()
{
    return ("你好,今天天气很不错!");
}
</script>
</head>

<body>
<h1>4,返回值的函数</h1>

<script language="javascript">
document.write(fun());
</script>
<p>
在body中的脚本调用函数。
</p>
<p>
这个函数返回一段文本。
</p>
</body>
</html>
JS:3.6,函数-调用外部js文件 返回顶部

 3.6.1, a1.js

function fun()
{
    alert("你好,今天天气很不错!");
}

3.6.2,

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script src="a1.js" language="javascript"></script>
</head>

<body>
<h1>
5,调用外部的js文件
</h1>
<input type="button" onclick="fun()" value="调用函数" />
</body>
</html>

 

warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted on 2013-01-09 14:54  ylbtech  阅读(9921)  评论(0编辑  收藏  举报