Div+Css
JavaScript基础2
发布日期:2011-8-2
字号大小:12 14 16

                                                                                                                             <h1>函数</h1>
<h1>1.函数</h1>

<html>
<head>

<script type="text/javascript">
function myfunction()
{
alert("您好!")
}
</script>

</head>
<body>

<form>
<input type="button" onclick="myfunction()" value="使用函数">
</form>

<p>通过点击这个按钮,可以调用一个函数。该函数会提示一条消息。</p>

</body>
</html>


<h1>2.带有参数的函数</h1>

<html>
<head>

<script type="text/javascript">
function myfunction(txt)
{
alert(txt)
}
</script>

</head>
<body>

<form>
<input type="button" onclick="myfunction('您好!')" value="使用函数">
</form>

<p>通过点击这个按钮,可以调用一个带参数的函数。该函数会输出这个参数。</p>

</body>
</html>


<h1>3.带有参数的函数2</h1>

 

<html>
<head>
<script type="text/javascript">
function myfunction(txt)
{
alert(txt)
}
</script>
</head>

<body>
<form>
<input type="button"
onclick="myfunction('早安!')"
value="早晨">

<input type="button"
onclick="myfunction('晚安!')"
value="夜晚">
</form>

<p>通过点击这个按钮,可以调用一个函数。该函数会输出传递给它的参数。</p>

</body>
</html>

 

<h1>4.返回值的函数</h1>


<html>
<head>

<script type="text/javascript">
function myFunction()
{
return (" .......!")
}
</script>

</head>
<body>

<script type="text/javascript">
document.write(myFunction())
</script>

<p>body 部分中的脚本调用一个函数。</p>

<p>该函数返回一段文本。</p>

</body>
</html>


<h1>5.带有参数并返回值的函数</h1>

 

<html>
<head>
<script type="text/javascript">
function product(a,b)
{
return a*b
}
</script>
</head>

<body>
<script type="text/javascript">
document.write(product(8,8))
</script>

<p>body 部分中的脚本调用一个带有两个参数(8 和 8)的函数。</p>
<p>该函数会返回这两个参数的乘积。</p>

</body>
</html>

 

<h1>这一节专门讲函数,函数也不算难.

将脚本编写为函数,就可以避免页面载入时执行该脚本。

函数包含着一些代码,这些代码只能被事件激活,或者在函数被调用时才会执行。</h1>