JavaScript 函数

本文提供了多个JavaScript函数实例,包括无参函数、带参函数、返回值函数等,展示了如何在HTML中使用JavaScript进行交互操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

函数

 

<html>
<head>

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

</head>
<body>

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

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

</body>
</html>


一个带参数的函数

<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

 

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


 

带有参数并返回值的函数

 

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

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

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

</body>
</html>


 

 

 

转载于:https://my.oschina.net/u/2552902/blog/543837

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值