JavaScript 10.29
1.1函数的概述
我们现在的计算机科学 还没有脱离数学的范畴。 是应用数学的另一种表现形式。 计算中很多的术语都源于数学。所以 函数这个名词其实也是源于数学。
我们之前学过很多语句,例如
if(){} 条件语句 , 控制大括号代码块 是否执行。
for(){} 循环语句, 控制大括号代码块 执行多少次。
function(){} 函数,控制大括号代码块 何时执行。 函数实在调用的时候才会去执行。
1.2 函数的声明和调用
函数声明:
function 名字(){
代码块
}
函数调用:
函数名();
<script type="text/javascript">
/* 函数声明之后 代码是不执行的 */
function haha(){
document.write("123");
document.write("123");
document.write("123");
}
/* 只有调用函数的时候 才会去执行 并且调用多少次 就 执行多少次 */
haha();
haha();
haha();
</script>
1.3 函数事件绑定
点击页面按钮 改变背景颜色
<body>
<button onclick="haha()">改变</button>
<!-- ---------------------------------------------------------------------------------------------------- -->
<script type="text/javascript">
/* who button when onclick what 改变背景颜色 */
function haha(){
document.body.style.backgroundColor = "red";
}
</script>
</body>
点击按钮改变图片:
<button onclick="haha1()">红</button>
<button onclick="haha2()">黄</button>
<button onclick="haha3()">蓝</button>
<button onclick="haha4()">绿</button>
<img id="foo" src="" width="200" height="200">
<!-- ---------------------------------------------------------------------------------------------------- -->
<script type="text/javascript">
function haha1(){
foo.src = "img/d_1.png";
}
function haha2(){
foo.src = "img/d_2.png";
foo.style.width = "800px";
}
function haha3(){
foo.src = "img/d_3.png";
}
function