js学习之路1: 初识js函数

本文通过具体实例,详细介绍了JavaScript中函数的使用,包括简单函数、带参数的函数、以及带返回值的函数。并通过实例展示了如何在HTML页面中调用这些函数。

1. 简单的函数:

<html>
<head>

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

</head>

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

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

</body>
</html>

 2.1 带参数的函数:

<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>

 2.2 带参数的函数:

<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>

 3.1 带返回值的函数

<html>
<head>

<script type="text/javascript">
function myfunction()
{
    return ("这一段话,会在结果中显示")
}
</script>
</head>

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

<p> body部分中的脚本调用一个函数。</p>
<script type="text/javascript">
document.write(myfunction())
</script>

</body>
</html>

3.2 带有参数和返回值的函数

  尝试把返回值用字符串拼接的方式组合了一下,还真成功了

<html>
<head>

<script type="text/javascript">
function product(a, b, c)
{
    return "某食品厂" + a + "年度,生产了" + b + "万箱" + c
}
</script>


</head>

<body>

<script type="text/javascript">
document.write(product("2018", "15", "饼干"))
</script>

</body>
</html>

结果:

某食品厂2018年度,生产了15万箱饼干

 本文代码示例大多来自http://www.w3school.com.cn/b.asp,只为学习使用。

转载于:https://www.cnblogs.com/zrmw/p/10309100.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值