第26天:js-$id函數、焦點事件
阿新 • • 發佈:2017-09-09
返回值 效果 html 分享 code lan click 返回 return
一、函數return語句
定義函數的返回值,在函數內部用return來設置返回值,一個函數只能有一個返回值。同時,終止代碼的執行。
所有自定義函數默認沒有返回值
return後面不要換行
var a=10,b=20,c=30;
++a;
a++;
e=++a+(++b)+(c++)+a++;
alert(e);//77
二、獲得焦點、失去焦點事件
獲得焦點:onfocus
失去焦點:onblur
案例:
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>$id函數</title> 6 <style> 7 div{ 8 height: 100px; 9 width: 100px; 10 background-color: pink; 11 } 12 </style> 13 <script> 14 window.onload=function(){ 15 function $id(id){ 16 returndocument.getElementById(id); 17 } 18 $id("demo1").style.backgroundColor="red"; 19 $id("demo2").style.backgroundColor="yellow"; 20 $id("demo3").style.backgroundColor="blue"; 21 22 $id("btn").onclick=function(){ 23 if($id("txt").value=="鄧樂樂"){ 24 alert("恭喜,中獎了"); 25 }else{ 26 alert("查無此人"); 27 } 28 } 29 } 30 31 </script> 32 </head> 33 <body> 34 <div id="demo1"></div> 35 <div id="demo2"></div> 36 <div id="demo3"></div> 37 <input type="text" id="txt" value="請輸入..."> 38 <button id="btn">查詢</button> 39 </body> 40 </html>
運行效果:
第26天:js-$id函數、焦點事件