JavascriptÖÐevalº¯ÊýµÄÓ÷¨
ÓÐÐí¶àСÇÏÃÅÀ´Ê¹±à³Ì¸ü¼ÓÈÝÒס£ÆäÖÐÖ®Ò»¾ÍÊÇeval()º¯Êý£¬Õâ¸öº¯Êý¿ÉÒÔ°ÑÒ»¸ö×Ö·û´®µ±×÷Ò»¸öJavaScript±í´ïʽһÑùÈ¥Ö´ÐÐËü¡£ÒÔÏÂÊÇËüµÄ˵Ã÷
Eval º¯Êý
¹¦ÄÜ£ºÏȽâÊÍJavascript´úÂ룬ȻºóÔÚÖ´ÐÐËü
Ó÷¨£ºEval(codeString)
codeStringÊÇ°üº¬ÓÐJavascriptÓï¾äµÄ×Ö·û´®£¬ÔÚevalÖ®ºóʹÓÃJavascriptÒýÇæ±àÒë¡£
¾Ù¸öСÀý×Ó£º
var the_unevaled_answer = "2 + 3";
var the_evaled_answer = eval("2 + 3");
alert("the un-evaled answer is " + the_unevaled_answer + " and the evaled answer is " + the_evaled_answer);
Èç¹ûÄãÔËÐÐÕâ¶Îeval³ÌÐò, Ä㽫»á¿´µ½ÔÚJavaScriptÀï×Ö·û´®"2 + 3"ʵ¼ÊÉϱ»Ö´ÐÐÁË¡£ËùÒÔµ±Äã°Ñthe_evaled_answerµÄÖµÉè³É eval("2 + 3")ʱ, JavaScript½«»áÃ÷°×²¢°Ñ2ºÍ3µÄºÍ·µ»Ø¸øthe_evaled_answer¡£
Õâ¸ö¿´ÆðÀ´ËƺõÓеãɵ£¬Æäʵ¿ÉÒÔ×ö³öºÜÓÐȤµÄÊ¡£±ÈÈçʹÓÃevalÄã¿ÉÒÔ¸ù¾ÝÓû§µÄÊäÈëÖ±½Ó´´½¨º¯Êý¡£Õâ¿ÉÒÔʹ³ÌÐò¸ù¾Ýʱ¼ä»òÓû§ÊäÈëµÄ²»Í¬¶øʹ³ÌÐò±¾Éí·¢Éú±ä»¯£¬Í¨¹ý¾ÙÒ»·´Èý£¬Äã¿ÉÒÔ»ñµÃ¾ªÈ˵ÄЧ¹û¡£
ÔÚʵ¼ÊÖУ¬evalºÜÉÙ±»Óõ½£¬µ«Ò²ÐíÄã¼û¹ýÓÐÈËʹÓÃevalÀ´»ñÈ¡ÄÑÒÔË÷ÒýµÄ¶ÔÏó¡£ Îĵµ¶ÔÏóÄ£ÐÍ£¨DOM)µÄÎÊÌâÖ®Ò»ÊÇ:ÓÐʱÄãÒª»ñÈ¡ÄãÒªÇóµÄ¶ÔÏó¼òÖ±¾ÍÊÇÍ´¿à¡£ÀýÈ磬ÕâÀïÓÐÒ»¸öº¯ÊýѯÎÊÓû§Òª±ä»»ÄĸöͼÏó:±ä»»ÄĸöͼÏóÄã¿ÉÒÔÓÃÏÂÃæÕâ¸öº¯Êý:
function swapOne()
{
var the_image = prompt("change parrot or cheese","");
var the_image_object;
if (the_image == "parrot")
{
the_image_object = window.document.parrot;
}
else
{
the_image_object = window.document.cheese;
}
the_image_object.src = "ant.gif";
}
Á¬Í¬ÕâЩimage±ê¼Ç£º
<img src="/stuff3a/parrot.gif" name="parrot" />
<img src="/stuff3a/cheese.gif" name="cheese">
Çë×¢ÒâÏóÕâÑùµÄ¼¸ÐÐÓï¾ä£º
the_image_object = window.document.parrot;
Ëü°ÑÒ»¸öͼÏó¶ÔÏó·ó¸øÁËÒ»¸ö±äÁ¿¡£ËäÈ»¿´ÆðÀ´Óеã¶ùÆæ¹Ö£¬ËüÔÚÓï·¨ÉÏÈ´ºÁÎÞÎÊÌâ¡£µ«µ±ÄãÓÐ100¸ö¶ø²»ÊÇÁ½¸öͼÏóʱÔõô°ì£¿ÄãÖ»ºÃдÉÏÒ»´ó¶ÑµÄ if-then-elseÓï¾ä£¬ÒªÊÇÄÜÏóÕâÑù¾ÍºÃÁË£º
function swapTwo()
{
var the_image = prompt("change parrot or cheese","");
window.document.the_image.src = "ant.gif";
}
²»ÐÒµÄÊÇ, JavaScript½«»áÑ°ÕÒÃû×ֽРthe_image¶ø²»ÊÇÄãËùÏ£ÍûµÄ"cheese"»òÕß"parrot"µÄͼÏ
Ïà¹ØÎĵµ£º
ugmbbc·¢²¼ÓÚ 2009-11-14 09:41:43| 7628 ´ÎÔĶÁ ×ÖÌ壺´ó С ´òÓ¡Ô¤ÀÀ
var ReTitle = '»Ø¸´:Google ÅÅÃûÖÐµÄ 10 ¸ö×îÖøÃûµÄ JavaScript ¿â';
¸ÐлCOMSHARP CMSµÄͶµÝ
ÐÂÎÅÀ´Ô´:tripwiremagazine.com
JavaScript ÊÇ Web ¿ª·¢ÓëÉè¼ÆÖв»¿É»òȱµÄ¶«Î÷£¬²»¹ÜÊÇÒ»¸ö¼òµ¥µÄÍøÒ³»¹ÊÇÒ»¸öרҵµÄÕ¾µã£¬Ò²²»¹ÜÄãÊǸßÊÖ»¹ÊDzËÄñ ......
stringObj ±ØÑ¡Ïî¡£ÒªÖ´ÐиÃÌæ»»µÄ String ¶ÔÏó»òÎÄ×Ö¡£¸Ã¶ÔÏ󲻻ᱻ replace ·½·¨Ð޸ġ£
rgExp ±ØÑ¡Ïî¡£ÃèÊöÒª²éÕÒµÄÄÚÈݵÄÒ»¸öÕýÔò±í´ïʽ¶ÔÏó¡£
replaceText ±ØÑ¡Ïî¡£ÊÇÒ»¸öString ¶ÔÏó»òÎÄ×Ö£¬¶ÔÓÚstringObj ÖÐÿ¸öÆ¥Åä rgExp ÖеÄλÖö¼ÓøöÔÏóËù°üº¬µÄÎÄ×Ö¼ÓÒÔÌæ»»¡£
ÏÂÃæµÄʾÀýÑÝʾÁË replace ·½·¨ ......
javascript ¿ØÖÆ htmlÔªËØ ÏÔʾ/Òþ²Ø
1¡£±àдjsº¯Êý
<script type="text/javascript">
function display(y){$(y).style.display=($(y).style.display=="none")?"":"none";}
function $(s){return document.getElementById(s);}
</script>
2. ÒªÏÔʾ/Òþ²ØµÄhtmlÔªËؼÓÉÏ id ÊôÐÔ
< ......
B.1 anchor¶ÔÏó
¡¡¡¡Ê¹Óñê¼Ç´´½¨µÄHTMLÃèµãÄܱ»Ò»¸öÁ´½Ó×÷ΪĿ±êÈç¹ûêµã°üÀ¨HREF=ÌØÐÔ,ÔòËüÒ²ÊÇÒ»¸öÁ´½Ó¶ÔÏó¡£
¡¡¡¡anchor¶ÔÏóÊÇdocument¶ÔÏóµÄÒ»¸öÊôÐÔ,Ëü±¾ÉíûÓÐÊôÐÔ·½·¨»òÕßʼþ´¦Àí³ÌÐò¡£
B.2 anchorsÊý×é
¡¡¡¡anchorsÊý×éÊÇdocument¶ÔÏóµÄÒ»¸öÊôÐÔ,ÊÇÎĵµÄÚËùÓÐanchor¶ÔÏóµÄÒ»¸öÁбíÈç¹ûanchorÒ²ÊÇÒ»¸ölink ......
Ò» Undefined ÀàÐÍ
Ö»ÓÐÒ»ÖÖÖµ undefined
ÒÔÏÂÈýÖÖÇé¿ötypeof ·µ»ØÀàÐÍΪundefined
1. µ±±äÁ¿Î´³õʼ»¯Ê±
2. ±äÁ¿Î´¶¨Òåʱ
3. º¯ÊýÎÞÃ÷È··µ»Øֵʱ£¨º¯ÊýûÓзµ»Øֵʱ·µ»ØµÄ¶¼ÊÇundefined£©
undefined ²¢²»µÈͬÓÚ䶨ÒåµÄÖµ
typeof ²¢²»ÕæÕýÇø·ÖÊÇ·ñÊÇ䶨Òå
¿´ÒÔÏÂʾÀý´úÂ룺
var oTemp;
alert(typeof o ......