变量和函数预解析'
<script type="text/javascript">
/*变量预解析*/
/*alert(a);//只把变量a的声明提前,赋值不提前,所以弹出undefined,表示它的值未定义
// alert(c);//报错,c没有声明,这是真正的未定义
var a = 123;
/*函数预解析*/
myalert();//弹出hello!
function myalert(){
alert('hello!');
}
</script>
..............................................................................................................................................................................
匿名函数
<script type="text/javascript">
window.onload = function(){
var oDiv = document.getElementById('div1');
/*有名字的函数*/
// oDiv.onclick = myalert;
// function myalert(){
// alert('hello');
// }
/*匿名函数*/
oDiv.onclick = function(){
alert('hello');
}
}
</script>
..............................................................................................................................................................................
函数传参
<script type="text/javascript">
window.onload = function(){
var oDiv = document.getElementById('div1');
changeStyle('color', 'gold');
changeStyle('background', 'red');
changeStyle('width', '300px');
changeStyle('height', '300px');
changeStyle('fontSize', '30px');
function changeStyle(styl, val){
oDiv.style[styl] = val;
}
}
</script>
.............................................................................................................................................................................
函数return关键字
<script type="text/javascript">
window.onload = function(){
var oInput01 = document.getElementById('input01');
var oInput02 = document.getElementById('input02');
var oBtn = document.getElementById('btn');
//写入值
// oInput01.value = 10;
// oInput02.value = 5;
oBtn.onclick = function(){
var val01 = oInput01.value;
var val02 = oInput02.value;
var rs = add(val01, val02);
alert(rs);
}
function add(a, b){
var c = parseInt(a) + parseInt(b);
// alert('计算完成');//执行
return c;//返回函数设定的值,同时结束函数的运行
// return;//不返回值,仅结束函数的运行
// alert('计算完成');//不执行
}
}
</script>
............................................................................................................................................
<body>
<input id="input01" type="text" name="">
<input id="input02" type="text" name="">
<input id="btn" type="button" name="" value="相加">
</body>