JavaScriptÃæÏò¶ÔÏó³ÌÐòÉè¼Æ(4): º¯Êý
ÔںܶàÓïÑÔÖУ¬º¯Êý(JavaÀïÃæ³ÉΪ·½·¨)ºÍ¶ÔÏóʱ½ØÈ»²»Í¬µÄÁ½ÖÖ¶«Î÷¡£º¯Êý±»¶¨ÒåΪ¶ÔÏóµÄ¶¯×÷£¬»òÕßÊÇÈ«¾ÖµÄ(ÏñÔÚC++ÖеÄmainº¯ÊýÒ»Ñù)¡£µ«ÊÇÔÚJavaScriptÖУ¬º¯ÊýºÍ¶ÔÏóµÄ½çÏÞÈ´ÏԵò»ÄÇôÃ÷ÏÔ¡£
1. º¯ÊýµÄ¶¨Òå
JavaScriptÖÐÓкܶàÖÖ¶¨Ò庯ÊýµÄ·½·¨£º
Js´úÂë
function hello() { alert("Hello!"); }
var hello1 = function() { alert("Hello!"); };
var hello2 = new Function("", "alert('Hello!');");
hello();
hello1();
hello2();
ÉÏÃæ¸ø³öÁËÈýÖÖJavaScriptµÄº¯Êý¶¨ÒåÓï¾ä¡£µÚÒ»¾äÊdz£¼ûµÄ¶¨Ò壬¿´ÉÏÈ¥ºÍJavaµÈÓïÑÔûÓÐÌ«´óµÄ²»Í¬¡£Õâ¾äÊǶ¨ÒåÁËÒ»¸ö¾ßÃûº¯Êý£¬°´ÕÕÉÏÃæµÄÀý×Ó£¬ÕâÀïµÄº¯Êý¶¨ÒåÃû×ÖΪhello¡£µÚ¶þ¾äÊǽ«Ò»¸öÄäÃûº¯Êý¶¨ÒåºÃºó¸³Öµ¸øÒ»¸ö±äÁ¿£¬ÓÚÊÇͨ¹ýÕâ¸ö±äÁ¿¾Í¿ÉÒÔÒýÓÃÕâ¸öÄäÃûº¯Êý¡£ÕâÁ½¾ä¿´ÉÏȥЧ¹û²î²»¶à£¬µ«ÊÇËüÃÇÊDz»Ò»ÑùµÄ£ºµÚÒ»¾ä¶¨ÒåµÄÊÇÒ»¸ö¾ßÃûº¯Êý£¬µÚ¶þ¾ä¶¨ÒåµÄÊÇÒ»¸öÄäÃûº¯Êý——¾¡¹ÜÄã¿ÉÒÔͨ¹ýÕâ¸ö±äÁ¿ÒýÓõ½Õâ¸öÄäÃûº¯Êý£¬µ«Êµ¼ÊÉÏËü»¹ÊÇÄäÃûµÄ¡£ËüÃǵÄÇø±ð¿ÉÒÔÓÉÏÂÃæµÄ¿´³ö£º
Js´úÂë
hello();
hello1(); // error
function hello() { alert("Hello!"); }
var hello1 = function() { alert("Hello!"); };
¾ßÃûº¯ÊýµÄ×÷Ó÷¶Î§ÊÇÈ«¾ÖµÄ£ºÄã¿ÉÒÔÔÚ¶¨Òå֮ǰʹÓÃÕâ¸öº¯Êý¡£µ«ÊÇÄäÃûº¯ÊýµÄ¶¨ÒåÊǺóÏòµÄ£¬ÏñC/C++Ò»Ñù£¬±ØÐëÔÚ¶¨ÒåÖ®ºó²ÅÄÜʹÓá£Õâ¾ÍÊÇΪʲôhello¿ÉÒÔʹÓ㬵«ÊÇhello1¾Í»áÓдíÎó¡£È»ºóÊÔÏëÒ»ÏÂÕâÊÇΪʲôÄØ£¿JavaScriptµÄ½âÊ͹ý³ÌºÍHTMLÒ»ÑùÊÇ´ÓÉϵ½Ïµġ£ËùÒÔ£¬ÕâÀïµÄÄäÃûº¯Êý¾ÍÏ൱ÓÚÊÇÒ»¸ö±äÁ¿µÄ¶¨Ò壬Òò´ËÔÚJavaScript½âÊÍÆ÷½âÊÍÖ´ÐÐʱ²¢²»ÖªµÀÕâ¸ö±äÁ¿µÄ¶¨Ò壬Òò´Ë·¢Éú´íÎó¡£µ«ÊÇ£¬¶ÔÓÚº¯ÊýµÄ¶¨ÒåÔòÊÇɨÃèÈ«¾Ö¡£
µÚÈý¸öÓï¾ä¾ÍºÜÓÐÒâ˼ÁË¡£Ëü´´½¨ÁËÒ»¸öFunctionÀàµÄ¶ÔÏó¡£Õâ¸ö¹¹Ô캯Êý(¹ÃÇÒÕâô½Ð°É)¾ßÓÐÁ½¸ö²ÎÊý£¬µÚÒ»¸öÊǺ¯ÊýµÄ²ÎÊý£¬µÚ¶þ¸öÊǺ¯ÊýÌå¡£¾ßÌåÀ´Ëµ£¬ÏÂÃæµÄÁ½¸öº¯Êý¶¨ÒåÊǵȼ۵ģº
Js´úÂë
function sayHelloTo(name) {
alert("Hello, " + name);
}
var sayHelloTo1 = new Function("name", "alert('Hello, ' + name)");
ÕâÖÖʹÓÃFunction½øÐж¨ÒåµÄ·½Ê½²¢²»³£¼û£¬µ«ÊÇÕâ¸öÓï¾äÏÔʾµÄÌØÐÔÈ´ºÜÓÐȤ£ºËüÒâζ×Å£¬Äã¿ÉÒÔʹÓÃÕâÖÖ¹¹Ô캯ÊýÔÚÔËÐÐʱ¶¯Ì¬µÄ¹¹Ô캯Êý£¡ÕâÊÇÒ»°ãµÄÓïÑÔûÓеÄÌØÐÔ¡£
2. º¯ÊýµÄ²ÎÊý
JavaScriptµÄº¯ÊýÒ²ÊÇÏ൱µÄÁé»î£¬²»½öÊÇËüµÄ¶¨Ò巽ʽ¶àÖÖ¶àÑù£¬ÉõÖÁËüµÄ²ÎÊý¶¼ÓГÆæ¹Ö”µÄÐÐΪ¡£ÓÉÓÚJavaScriptÊÇÈõÀà
Ïà¹ØÎĵµ£º
scrollHeight: »ñÈ¡¶ÔÏóµÄ¹ö¶¯¸ß¶È¡£
scrollLeft:ÉèÖûò»ñȡλÓÚ¶ÔÏó×ó±ß½çºÍ´°¿ÚÖÐÄ¿Ç°¿É¼ûÄÚÈݵÄ×î×ó¶ËÖ®¼äµÄ¾àÀë
scrollTop:ÉèÖûò»ñȡλÓÚ¶ÔÏó×¶ËºÍ´°¿ÚÖпɼûÄÚÈݵÄ×¶ËÖ®¼äµÄ¾àÀë
scrollWidth:»ñÈ¡¶ÔÏóµÄ¹ö¶¯¿í¶È
offsetHeight:»ñÈ¡¶ÔÏóÏà¶ÔÓÚ°æÃæ»òÓɸ¸×ø±ê offsetParent ÊôÐÔÖ¸¶¨µÄ¸¸×ø±êµÄ¸ß¶È
offsetL ......
¾³£»áÔÚjsÀïÓõ½Êý×é,±ÈÈç ¶à¸öÃû×ÖÏàͬµÄinput, ÈôÊǶ¯Ì¬Éú³ÉµÄ, Ìύʱ¾ÍÐèÒªÅжÏÆäÊÇ·ñÊÇÊý×é.
if(document.mylist.length != "undefined" ) {} Õâ¸öÓ÷¨ÓÐÎó.
ÕýÈ·µÄÊÇ if( typeof(document.mylist.length) != "undefined" ) {}
»ò if( !isNaN(document.mylist.length) ) {}
typeofµÄÔËËãÊý䶨Òå,·µ»ØµÄ¾ÍÊÇ "u ......
JavaScript eval() º¯Êý
¶¨ÒåºÍÓ÷¨
eval() º¯Êý¿É¼ÆËãij¸ö×Ö·û´®£¬²¢Ö´ÐÐÆäÖÐµÄµÄ JavaScript ´úÂë¡£
Óï·¨
eval(string)
²ÎÊýÃèÊö
string
±ØÐè¡£Òª¼ÆËãµÄ×Ö·û´®£¬ÆäÖк¬ÓÐÒª¼ÆËãµÄ JavaScript ±í´ïʽ»òÒªÖ´ÐеÄÓï¾ä¡£
·µ»ØÖµ
ͨ¹ý¼ÆËã string µÃµ½µÄÖµ£¨Èç¹ûÓеĻ°£©¡£
˵Ã÷
¸Ã·½·¨Ö»½ÓÊÜÔʼ×Ö·û´®×÷Ϊ² ......
10:33:41 2009-11-14
function checkDate() {
//È¡µÃdateµÄÖµ
if(!document.getElementById || !document.createTextNode()){return;}
var dateFiled = document.getElementById('date');
if(!dateFiled) {return;}
var dateValue = dateFiled.value;
var checkPattern = new RegExp("\\d{2}/\\d{2}/\\d ......