JavaScript 函数的使用技巧

2025-11-03 14:53:46

1、最简单的函数流程。我们定义一个函数,再设计一个按钮,让访问者可以实现点击按钮就有函数调用出来。

<html>

<head>

<script type="text/javascript">

function 函数()

{

alert("天天快乐")

}

</script>

</head>

<body>

<form>

<input type="button" onclick="函数()" value="函数按钮">

</form>

只要点击按钮就会调用函数

</body>

</html>

JavaScript 函数的使用技巧

2、案例二:设计一个有变量的函数,再让参数值赋予这个函数。

<html>

<head>

<script type="text/javascript">

function 函数(变量)

{

alert(变量)

}

</script>

</head>

<body>

<form>

<input type="button" onclick="函数('天天快乐')" value="给函数数值">

</form>

点击按钮就会让函数有参数值

</body>

</html>

JavaScript 函数的使用技巧

3、向一个函数传递两个不同的参数值的java 代码使用技巧。

<html> 

<head> 

<script type="text/javascript"> 

function 函数(变量) 

alert(变量) 

</script> 

</head> 

<body> 

<form> 

<input type="button" 

onclick="函数('天天快乐')" 

value="百度"> 

<input type="button" 

onclick="函数('好心情')" 

value="经验"> 

</form> 

向同样的函数传递不同的参数值

</body> 

</html>

JavaScript 函数的使用技巧

4、从上面的例子可以看出来,点击不同的窗口可以呈现不同的消息框结果,这就是一个函数被赋予不同参数值的结果。

JavaScript 函数的使用技巧

5、直接让函数返回一个参数值。

<html>

<head>

<script type="text/javascript">

function 函数()

{

return ("天天快乐")

}

</script>

</head>

<body>

<script type="text/javascript">

document.write(函数())

</script>

</body>

</html>

JavaScript 函数的使用技巧

6、设计一个乘法函数,给其不同的参数值,让其运算结果呈现在网页上面。

<html>

<head>

<script type="text/javascript">

function 函数(a,b)

{

return a*b

}

</script>

</head>

<body>

<script type="text/javascript">

document.write(函数(4,4))

document.write(函数(5,5))

document.write(函数(6,6))

</script>

</body>

</html>

JavaScript 函数的使用技巧

7、除法也是一样的!

<html>

<head>

<script type="text/javascript">

function 函数(a,b)

{

return a/b

}

</script>

</head>

<body>

<script type="text/javascript">

document.write(函数(4,2))

document.write(函数(10,5))

document.write(函数(12,6))

</script>

</body>

</html>

JavaScript 函数的使用技巧

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢