JAVASCRIPT學習筆記基礎(二)
函數
JavaScript 函數
將腳本編寫為函數,就可以避免頁麵載入時執行該腳本。
函數包含著一些代碼,這些代碼隻能被事件激活,或者在函數被調用時才會執行。
你可以在頁麵中的任何位置調用腳本(如果函數嵌入一個外部的 .js 文件,那麼甚至可以從其他的頁麵中調用)。
函數在頁麵起始位置定義,即 <head> 部分。
<html>
<head>
<script type="text/javascript">
function myfunction()
{
alert("您好!");
}
</script>
</head>
<body>
<form>
<input type="button" value="調用函數">
</form>
<p>通過點擊這個按鈕,可以調用一個函數。該函數會提示一條消息</p>
</body>
</html>
帶參數的函數
<html>
<head>
<script type="text/javascript">
function myfunction(txt)
{
alert(txt)
}
</script>
</head>
<body>
<form>
<input type="button" value="調用函數">
</form>
<p>通過點擊這個按鈕,可以調用一個帶參數的函數。該函數會輸出這個參數。</p>
</body>
</html>
帶參數的函數2
<html>
<head>
<script type="text/javascript">
function myfunction(txt)
{
alert(txt)
}
</script>
</head>
<body>
<form>
<input type="button" value="在早晨">
<input type="button" value="在夜晚">
</form>
<p>通過點擊這個按鈕,可以調用一個函數。該函數會輸出傳遞給它的參數。</p>
</body>
</html>
返回值的函數
<html>
<head>
<script type="text/javascript">
function myFunction()
{
return ("您好,祝您愉快!")
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(myFunction())
</script>
<p>body 部分中的腳本調用一個函數。</p>
<p>該函數返回一段文本。</p>
</body>
</html>
帶參數和返回值的函數
<html>
<head>
<script type="text/javascript">
function product(a,b)
{
return a*b
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(product(6,5))
</script>
<p>body 部分中的腳本調用一個帶有兩個參數(6 和 5)的函數。</p>
<p>該函數會返回這兩個參數的乘積。</p>
</body>
</html>
for循環
<html>
<body>
<script type="text/javascript">
for(i=0;i<=5;i++)
{
document.write("數字是"+i)
document.write("<br />")
}
</script>
<h1>解釋:</h1>
<p>for 循環的步進值從 i=0 開始。</p>
<p>隻要 <b>i</b> 小於等於 5,循環就會繼續運行。</p>
<p>循環每循環一次,<b>i</b> 就會累加 1。</p>
</body>
</html>
循環產生HTML標題
<html>
<body>
<script type="text/javascript">
for(i=1;i<=6;i++)
{
document.write("<h"+i+">這是標題"+i)
document.write("</h"+i+">")
}
</script>
</body>
</html>
一個for循環程序
<html>
<body>
<script type="text/javascript">
var i=0
for(i=0;i<10;i++)
{
document.write("The number is "+i);
document.write("<br />")
}
</script>
</body>
</html>
while循環
<html>
<body>
<script type="text/javascript">
i = 0
while (i <= 5)
{
document.write("數字是 " + i)
document.write("<br />")
i++
}
</script>
<h1>解釋:</h1>
<p><b>i</b> 等於 0。</p>
<p>當 <b>i</b> 小於或等於 5 時,循環將繼續運行。</p>
<p>循環每運行一次,<b>i</b> 會累加 1。</p>
</body>
</html>
dowhile循環
<html>
<body>
<script type="text/javascript">
var i=0
do
{
document.write("The number is "+i)
document.write("<br />")
i=i+1
}
while(i<0)
</script>
</body>
</html>
dowhile循環2
<html>
<body>
<script type="text/javascript">
i = 0
do
{
document.write("數字是 " + i)
document.write("<br />")
i++
}
while (i <= 5)
</script>
<h1>解釋:</h1>
<p><b>i</b> 等於 0。</p>
<p>循環首先會運行。</p>
<p>每循環一次,<b>i</b> 就會累加 1。</p>
<p>當 <b>i</b> 小於或等於 5 時,循環會繼續運行。</p>
</body>
</html>
break語句
<html>
<body>
<script type="text/javascript">
var i=0
for(i=0;i<=10;i++)
{
if(i==3) {break}
document.write("數字是 "+i)
document.write("<br />")
}
</script>
<p>解釋:循環會在 i=3 時中斷。</p>
</body>
</html>
輸出:
數字是 0
數字是 1
數字是 2
解釋:循環會在 i=3 時中斷。
使用 continue 語句來終止當前的循環,然後從下一個值繼續執行。
<html>
<body>
<script type="text/javascript">
var i=0
for(i=0;i<=10;i++)
{
if(i==3) {continue}
document.write("數字是 "+i)
document.write("<br />")
}
</script>
<p>解釋:循環會在 i=3 時繼續循環。</p>
</body>
</html>
輸出:
數字是 0數字是 1數字是 2數字是 4數字是 5數字是 6數字是 7數字是 8數字是 9數字是 10
解釋:循環會在 i=3 時繼續循環。
JavaScript For...In 聲明
For...In 聲明用於對數組或者對象的屬性進行循環操作。
for ... in 循環中的代碼每執行一次,就會對數組的元素或者對象的屬性進行一次操作。
語法:
for(變量in對象) { 在此執行代碼 }
“變量”用來指定變量,指定的變量可以是數組元素,也可以是對象的屬性。
<html>
<body>
<script type="text/javascript">
var x
var mycars=new Array()
mycars[0]="寶馬"
mycars[1]="奔馳"
mycars[2]="賓利"
for(x in mycars)
{
document.write(mycars[x]+"<br />")
}
</script>
</body>
</html>
<html>
<body>
<script type="text/javascript">
var i=0
for(i=0;i<=10;i++)
{
if(i==3) {continue}
document.write("數字是 "+i)
document.write("<br />")
}
</script>
<p>解釋:循環會在 i=3 時繼續循環。</p>
</body>
</html>
輸出:
數字是 0數字是 1數字是 2數字是 4數字是 5數字是 6數字是 7數字是 8數字是 9數字是 10
解釋:循環會在 i=3 時繼續循環。
最後更新:2017-04-02 06:52:21