{"id":12,"date":"2007-05-20T23:57:13","date_gmt":"2007-05-21T03:57:13","guid":{"rendered":"http:\/\/prglab.com\/blog\/?page_id=12"},"modified":"2009-02-25T10:06:01","modified_gmt":"2009-02-25T14:06:01","slug":"js-switch","status":"publish","type":"page","link":"https:\/\/www.prglab.com\/blog\/js-tutorial\/js-switch","title":{"rendered":"JavaScript\u4e2d\u7684 Switch \u8bed\u53e5"},"content":{"rendered":"<p>Switch\u8bed\u53e5\u662f\u4e00\u79cd\u6761\u4ef6\u8bed\u53e5\uff0c\u7528\u6765\u5728\u4e0d\u540c\u6761\u4ef6\u4e0b\u6267\u884c\u4e0d\u540c\u7684\u52a8\u4f5c\u3002<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n<strong>JavaScript\u4e2d\u7684 Switch \u8bed\u53e5<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u60f3\u5728\u591a\u7ec4\u8bed\u53e5\u4e2d\u53ea\u9009\u51fa\u4e00\u7ec4\u6765\u6267\u884c\uff0c\u4f7f\u7528switch\u8bed\u53e5\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<table width=\"96%\" align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\">\n<tr>\n<td class=\"quote\">switch(n)<br \/>\n{<br \/>\ncase 1:<br \/>\n\u6267\u884c\u8bed\u53e5\u5757 1<br \/>\nbreak<br \/>\ncase 2:<br \/>\n\u6267\u884c\u8bed\u53e5\u5757 2<br \/>\nbreak<br \/>\ndefault:<br \/>\n\u5982\u679c n \u65e2\u4e0d\u540c\u4e8e\u60c5\u51b5 1 \u4e5f\u4e0d\u540c\u4e8e\u60c5\u51b5 2 \u65f6\u8981\u6267\u884c\u7684\u8bed\u53e5\u5757<br \/>\n}<\/td>\n<\/tr>\n<\/table>\n<p>switch \u8bed\u53e5\u662f\u8fd9\u6837\u5de5\u4f5c\u7684\uff1a\u9996\u5148\u6211\u4eec\u6709\u4e00\u4e2a\u8868\u8fbe\u5f0f n (\u901a\u5e38\u662f\u4e00\u4e2a\u53d8\u91cf), \u5b83\u88ab\u8ba1\u7b97\u4e00\u6b21\uff0c\u8ba1\u7b97\u7ed3\u679c\u7684\u503c\u88ab\u7528\u6765\u548c\u7ed3\u6784\u4e2d\u6bcf\u4e00\u79cd\u60c5\u51b5(case)\u7684\u503c\u4f5c\u6bd4\u8f83\u3002\u5982\u679c\u6709\u4e00\u4e2acase\u7684\u503c\u76f8\u7b26\uff0c\u8be5case\u4e0b\u9762\u8054\u7cfb\u7684\u4e00\u5757\u4ee3\u7801\u5c06\u4f1a\u88ab\u6267\u884c\u3002\u6ce8 \u610f\u8981\u4f7f\u7528break\u6765\u9632\u6b62\u4ee3\u7801\u81ea\u52a8\u6267\u884c\u5230\u4e0b\u9762\u4e00\u4e2acase\u4e2d\u53bb\u3002<\/p>\n<p><strong>\u4f8b\u5b50<\/strong><\/p>\n<table width=\"96%\" align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\">\n<tr>\n<td class=\"quote\">&lt;script type=&#8221;text\/javascript&#8221;&gt;<br \/>\n\/\/\u6839\u636e\u661f\u671f\u51e0\u7684\u4e0d\u540c\u663e\u793a\u4e0d\u540c\u7684\u95ee\u5019\u8bed<br \/>\n\/\/\u6ce8\u610f\uff1a\u661f\u671f\u65e5=0, \u661f\u671f\u4e00=1, \u661f\u671f\u4e8c=2, \u7b49\u7b49.<\/p>\n<p>var d=new Date()<br \/>\ntheDay=d.getDay()<br \/>\nswitch (theDay)<br \/>\n{<br \/>\ncase 5:<br \/>\ndocument.write(&#8220;Finally Friday&#8221;)<br \/>\nbreak<br \/>\ncase 6:<br \/>\ndocument.write(&#8220;Super Saturday&#8221;)<br \/>\nbreak<br \/>\ncase 0:<br \/>\ndocument.write(&#8220;Sleepy Sunday&#8221;)<br \/>\nbreak<br \/>\ndefault:<br \/>\ndocument.write(&#8220;I&#8217;m looking forward to this weekend!&#8221;)<br \/>\n}<br \/>\n&lt;\/script&gt;<\/td>\n<\/tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Switch\u8bed\u53e5\u662f\u4e00\u79cd\u6761\u4ef6\u8bed\u53e5\uff0c\u7528\u6765\u5728\u4e0d\u540c\u6761\u4ef6\u4e0b\u6267\u884c\u4e0d\u540c\u7684\u52a8\u4f5c\u3002 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- JavaScript\u4e2d\u7684 Switch \u8bed\u53e5 \u5982\u679c\u4f60\u60f3\u5728\u591a\u7ec4\u8bed\u53e5\u4e2d\u53ea\u9009\u51fa\u4e00\u7ec4\u6765\u6267\u884c\uff0c\u4f7f\u7528switch\u8bed\u53e5\u3002 \u8bed\u6cd5 switch(n) { case 1: \u6267\u884c\u8bed\u53e5\u5757 1 break case 2: \u6267\u884c\u8bed\u53e5\u5757 2 break default: \u5982\u679c n \u65e2\u4e0d\u540c\u4e8e\u60c5\u51b5 1 \u4e5f\u4e0d\u540c\u4e8e\u60c5\u51b5 2 \u65f6\u8981\u6267\u884c\u7684\u8bed\u53e5\u5757 } switch \u8bed\u53e5\u662f\u8fd9\u6837\u5de5\u4f5c\u7684\uff1a\u9996\u5148\u6211\u4eec\u6709\u4e00\u4e2a\u8868\u8fbe\u5f0f n (\u901a\u5e38\u662f\u4e00\u4e2a\u53d8\u91cf), \u5b83\u88ab\u8ba1\u7b97\u4e00\u6b21\uff0c\u8ba1\u7b97\u7ed3\u679c\u7684\u503c\u88ab\u7528\u6765\u548c\u7ed3\u6784\u4e2d\u6bcf\u4e00\u79cd\u60c5\u51b5(case)\u7684\u503c\u4f5c\u6bd4\u8f83\u3002\u5982\u679c\u6709\u4e00\u4e2acase\u7684\u503c\u76f8\u7b26\uff0c\u8be5case\u4e0b\u9762\u8054\u7cfb\u7684\u4e00\u5757\u4ee3\u7801\u5c06\u4f1a\u88ab\u6267\u884c\u3002\u6ce8 \u610f\u8981\u4f7f\u7528break\u6765\u9632\u6b62\u4ee3\u7801\u81ea\u52a8\u6267\u884c\u5230\u4e0b\u9762\u4e00\u4e2acase\u4e2d\u53bb\u3002 \u4f8b\u5b50 &lt;script type=&#8221;text\/javascript&#8221;&gt; \/\/\u6839\u636e\u661f\u671f\u51e0\u7684\u4e0d\u540c\u663e\u793a\u4e0d\u540c\u7684\u95ee\u5019\u8bed \/\/\u6ce8\u610f\uff1a\u661f\u671f\u65e5=0, \u661f\u671f\u4e00=1, \u661f\u671f\u4e8c=2, \u7b49\u7b49. var d=new Date() theDay=d.getDay() switch (theDay) { case 5: document.write(&#8220;Finally Friday&#8221;) break case [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3,"menu_order":5,"comment_status":"open","ping_status":"open","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/12"}],"collection":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/comments?post=12"}],"version-history":[{"count":0,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/12\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/3"}],"wp:attachment":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/media?parent=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}