1.js函數(shù)
function aa(){
alert('hello!');
}
/*
//直接調(diào)用
aa();
*/
</script>
2.js可控制換膚
window.onload = function(){
/* 提取行間事件 */
var oBtn01 = document.getElementById('btn01');
var oBtn02 = document.getElementById('btn02');
oBtn01.onclick = skin01;//這里不能寫skin01(),這樣寫就馬上執(zhí)行了
oBtn02.onclick = skin02;
}
function skin01(){
var oLink = document.getElementById('link1');
oLink.href = "css/1.css";
}
function skin02(){
var oLink = document.getElementById('link1');
oLink.href = "css/2.css";
}
3.變量和函數(shù)預(yù)解析
/*變量預(yù)解析*/
/*alert(a);//只把變量a的聲明提前,賦值不提前撒蟀,所以彈出undefined,表示它的值未定義
// alert(c);//報錯,c沒有聲明剩胁,這是真正的未定義
var a = 123;
/*函數(shù)預(yù)解析*/
myalert();//彈出hello!
function myalert(){
alert('hello!');
}
4.匿名函數(shù)
window.onload = function(){
var oDiv = document.getElementById('div1');
/*有名字的函數(shù)*/
// oDiv.onclick = myalert;
// function myalert(){
// alert('hello');
// }
/*匿名函數(shù)*/
oDiv.onclick = function(){
alert('hello');
}
}
5.函數(shù)參數(shù)
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;
}
}