寫法:
<script>
$(document).ready(function(){
//呼叫函式
hello();
});
//自己寫一個函式,名稱是hello()
function hello(){
//輸出內容
alert("哈囉!大家好!");
//document.write("哈囉!大家好!");
}
</script>
範例:求長方形面積的函式
<script>
$(document).ready(function(){
square(6,8);
});
//宣告這個函式 squre有二個參數 a和b
function square(a,b){
alert("長方形面積是:"+a*b);
}
</script>
結果:長方形面積是:48
<script>
$(document).ready(function(){
alert("長方形面積是:"+square(6,8));
});
function square(a,b){
return a*b;
}
</script>
<script>
$(document).ready(function(){
var hello = function(){ alert("你好!"); }
alert(hello())
});
</script>
<script>
$(document).ready(function(){
var square = function(a,b){ return a*b; }
alert("長方形面積是:"+square(6,8))
});
</script>
<script>
$(document).ready(function(){
var hello = () => alert(123);
hello();
});
</script>
<script>
$(document).ready(function(){
//省略return關鍵字
var square = (a,b) => a*b;
alert("長方形面積是:"+square(6,8))
});
</script>
範例:
<script>
$(document).ready(function(){
});
</script>