函数定义好后,是不能自动执行的,需要调用它,直接在需要的位置写函数名。
第一种情况:在<script>标签内调用。
<script type="text/javascript"> function add2() { sum = 1 + 1; alert(sum); } add2(); //调用函数,直接写函数名 </script>
第二种情况:在HTML文件中调用,如通过点击按钮后调用定义好的函数。
<html> <head> <script type="text/javascript"> function add2() { sum = 5 + 6; alert(sum); } </script> </head> <body> <form> <input type="button" value="click it" ="add2()"> //按钮,点击事件,直接写函数名 </form> </body> </html>