ºô­¶¼ÐÃD¡G ª«¥ó (Object)
¡@
¡T¡T¡TÆ[¬Ý¯d¨¥¡G¦¹¤å³¹¤w¸g¦³1«h¯d¨¥ ¡T¡T¡T


JavaScript ªì¶¥±Ð¾Ç

http://taiwantc.com/js/js_tut_a2.htm

ª«¥ó (Object)

ª«¥ó
ª«¥óªºªí¥Ü¤è¦¡
¤èªk»PÄÝ©Ê

ª«¥ó
¤j®a¦b²Ä¤@½g¤¤ªº Hello, World ¨Ò¤l¤¤ªì¦¸±µÄ²¨ì document ª«¥ó¡A ¦ýª«¥ó¬O¤°»ò©O¡H

JavaScript ¬Oª«¥ó¾É¦Vµ{¦¡»y¨¥¡A ²³æ¨Ó»¡§Y¬OÂsÄý¾¹¤ºªº¥ô¦óªF¦è³£µø§@ª«¥ó¡A ¦Ó¨C­Óª«¥ó¤S¥i¯à¥]§t (contain) ¨ä¥¦ª«¥ó¡C ¤j®a¥i¥H¥Î¤Q¬í®É¶¡·Q¤@·Q§AªºÂsÄý¾¹¤º¦³¨Ç¤°»òªF¦è¡A ¦pªG·Q¨ìªº¸Ü¥i¥H¦A·Q·Q³o¨ÇªF¦è¤S¥]§t¤F¤°»ò¨ä¥¦ªºªF¦è¡C

¬°¤FÅý¤j®a²M·¡ JavaScript ªºª«¥ó¬[ºc¡A ¦b³o¤@¸`¤º§Ú·|°w¹ï¥H¤U¹Ï¤ù¨Ó»¡©úª«¥ó¡G

¹Ï¤@¡G JavaScript ª«¥ó¼h¦¸¹Ï (JavaScript Object Hierarchy) ¨Ó·½¡G Netscape JavaScript Reference
¡]½sµù¡G­ì¹Ï½Ð°Ñ¾\­ì©l¤å¥óºô§}¡^

¹Ï¤@¬O Netscape ºô¯¸¤º¤@´T JavaScript ªºª«¥ó¼h¦¸¹Ï¡A ¨C­Óªø¤è§Î¥Nªí¤@­Óª«¥ó¡A ªø¤è§Î¤ºªº¤å¦rªí¥Ü¸Óª«¥óªº¦WºÙ (Name)¡A ³sôªø¤è§Îªº½uªí¥Ü¤Fª«¥ó»Pª«¥ó¤§¶¡ªº¼h¦¸Ãö«Y¡C

³Ì°ª¼h¦¸ªºª«¥ó¦³¨â­Ó¡G window¡@©M¡@navigator ¡C §Ú­Ì­º¥ý¬Ý¬Ý¡@window ¡A¥¦¥]§t¤F document ª«¥ó¡A §Y¬O»¡ window ¬O document ªº¥]§tªÌ (Container)¡A ¦A¬Ý¬Ý document ¡A ¥¦¥]§t¤F Form ª«¥ó¡A §Y¬O»¡ document ¬O Form ªº¥]§tªÌ¡C

ª«¥óªºªí¥Ü¤è¦¡
²{¦bª¾¹D­þ­Óª«¥ó¥]§t­þ­Óª«¥ó¡A ¨º»ò¦p¦óªí¥Ü¥¦­Ì©O¡H

·Qªí¥Ü¬Y­Óª«¥ó¡A ¥D­n¨BÆJ¦³¤T­Ó¡G

²Ä¤@¨B¡G §ä¥X¸Óª«¥óªº¦WºÙ¡A ¦WºÙ¬O«Ü­«­nªº¡C

­n§ä¥X¤@­Óª«¥óªº¦WºÙ¡A ¥D­n¦³¨â­Ó¨BÆJ¡G

§ä¥X¸Óª«¥óªº HTML ¼Ð°OùØ name Äݩʪº­È¡A ¦Ó³o­Ó­È´N¬O¸Óª«¥óªº¦WºÙ¤F¡C ¨Ò¦p¦³¤@­Óªí³æ¡A ¥¦ªº HTML ½X¦p¤U¡G

<form name="my_form">
<input type="text" name="my_name" value="Your name here">
</form>

³o­Óªí³æª«¥óªº¦WºÙ´N¬O my_form ¡A ¦Óªí³æ¤ºªº¤å¦r¿é¤Jª«¥óªº¦WºÙ´N¬O my_name ¤F¡C

¦pªG¨º­Óª«¥ó¬O°ß¤@ªº (Unique)¡A ¦Ó¥B¥¦®Ú¥»¨S¦³ HTML ¼Ð°O¡A ¨º´N¥Î¸Óª«¥ó¦b¹Ï¤@ªº¦WºÙ¡C ¨Ò¦p document ¡A ¦]¬°¤@­ÓÂsÄý¾¹µøµ¡©Îµ¡Ï@¥u¯à¦³¤@­¶¡A ©Ò¥H document ¬O°ß¤@ªº¡A §Ú­Ì´N¥Î document §@¬° document ª«¥óªº¦WºÙ¡C ¨Ò¦p navigator (ÂsÄý¾¹ª«¥ó)¡A ¤@­ÓÂsÄý¾¹µøµ¡¥u¯àÄÝ©ó¸Ó­ÓÂsÄý¾¹¡A ¦]¦¹§Ú­Ì´N¥Î navigator §@¬° netvigator ª«¥óªº¦WºÙ¡C

²Ä¤G¨B¡G §ä¥X¸Óª«¥óªº©Ò¦³¥]§tªÌ¦WºÙ

¥u­n°Ñ¦Ò¹Ï¤@¡A «K«Ü©ö§ä¥X¬Y­Óª«¥óªº¥]§tªÌ¦WºÙ¡C ¦b¬Y¨Ç±¡ªp¤U¡A §AµL¶·§ä¥X¬Y¨Ç¥]§tªÌªº¦WºÙ¡A ¨Ò¦p¦b¬Y¤@­¶¤ºªº JavaScript ·Q¨Ï¥Î¸Ó­¶ªºª«¥ó¡A §A´N¤£¶·­n§ä¥X¨º¤@­¶©Ò¦bªºµøµ¡©Îµ¡Ï@ª«¥óªº¦WºÙ¡A ¦ý¦pªG§A·Q¦b¬Y­¶¨Ï¥Î¥t¤@­Óµøµ¡©Îµ¡Ï@ª«¥ó¡A ¨º§ä¥²¶·§ä¥X¨º­Óª«¥óªº¦WºÙ¤F¡C

²Ä¤T¨B¡G ¦bª«¥ó©M¥¦ªº©Ò¦³¥²¶·ªº¥]§tªÌ¤§¶¡¥Î¤@ÂI (.) ³s³e°_¨Ó ³o¨B§ó¥[®e©ö¡A ¨Ò¦p§A·Qªí¥Ü¤å¦r¿é¤Jª«¥ó¡A ¦WºÙ¬O my_name ¡A ¥¦¬O¦b¤@­Óªí³æ¤º¡A ¦WºÙ¬O my_form ¡A §Ú°²³]¨Ï¥Î³o­Óª«¥óªº JavaScript »P³o­Óª«¥óªº HTML ½X¬O¦b¦P¤@­¶¡A ¦]¦¹§Ú¤£¶·­n§ä¥Xµøµ¡ªº¦W¦r¡C ªí¥Ü¤è¦¡¦p¤U¡G

document.my_form.myname

¤j®a¥i¯à·|°Ý¡A ¬JµM¥¦­Ì¬O¦b¦P¤@­¶¡A ¨º»ò document ¤]¤£¶·­n¼g§a¡A ¥u¬O¼g my_form.myname ¤£´N¦æ¤F¶Ü¡H ²z­Û¤W¦n¹³¬O¹ïªº¡A §Ú¦b IE ©M Opera ´ú¸Õ¹L³oºØ¼gªk¡A ¥¦­Ì¤]¤ä´©¡A ¦ý¤£ª¾¦ó¸Ñ Netscape ·|²£¥Í¿ù»~¡A ©Ò¥H³oùØ«Øij¤j®a¤£­nº|¥h document ¡A ¦pªG§A©È¥´¤Ó¦h¦r¤]¤£¥Î¾á¤ß¡A ¤§«áªº½g³¹·|±Ð¤j®a¦p¦ó¹B¥ÎÅܼơA ¥O¦Û¤v¤£¥Î¥´³o»ò¦h¦r¡C

¤èªk»PÄÝ©Ê

¬Û«H¤j®a¤w¸g¦³¤Fª«¥óªº·§©À¤F¡A ¦ý¦³¤F¤@­Óª«¥ó¡A ¥i¥H°µ¨Ç¤°»ò©O¡H µª¡G ´N¬O¹B¥Î¥¦ªº¤èªk©MÄݩʤF¡G

¤èªk (Method)¡G
¥Î¨Ó°µ¤@¨Ç»P¸Óª«¥ó¦³Ãöªº°Ê§@¡A ¨Ò¦p document.write() ¡Awrite ¬O document ªº¤èªk¡A ©Ò¥H³o¤@¥y·|¦b document ¼g¤J¤@¨ÇªF¦è¡C

ªí¥Ü¤è¦¡¡G

Object_name.method()

¤èªk¥²¶·¦³¥ª¬A¸¹©M¥k¬A¸¹ ()¡A ¦Ó¬A¸¹¤º¬O¥Î¨Ó¿é¤J°Ñ¼Æªº¡C ·í¤èªk¨S¦³°Ñ¼Æ®É¡A ¤]À³¦b§À³¡¥[¤J¬A¸¹¡A ¥Hªí¥Ü¥¦¬O¤@­Ó¤èªk¡A ¦Ó¬A¸¹¤º¤£¥Î¼g¥ô¦óªF¦è¡C

¨ä¹ê¤èªk´N¦n¹³¤@­Ó¨ç¼Æ (Function)¡A ¤£¦P¤§³B¦b©ó¤èªk¬O³sôµÛª«¥óªº¡C ¨ç¼Æ·|¦b¤§«áªº½g³¹¸Ô­z¡C

ÄÝ©Ê (Property)¡G
¥Î¨Ó¨ú±o¸Óª«¥óªºÄݩʩθê°T¡A ¤]¥i¥H³]©w¬Y¨ÇÄݩʪº¤º®e¡C ¨Ò¦p document.bgColor ¡AbgColor ¬O document ªºÄÝ©Ê¡A ©Ò¥H³o¤@¥yªº¤º®e´N¬O¸Ó­Ó document ªº­I´ºÃC¦â¡A §A¤]¥i¥H³]©w¥¦ªº¤º®e¨Ó§ïÅÜ­I´ºÃC¦â¡A¨Ò¦p document.bgColor="silver" ¡A³o¥y·|§â¸Ó­¶ªº­I´ºÃC¦âÂର»È¦â¡C

ªí¥Ü¤è¦¡¡G

Object_name.property

¨ä¹êÄÝ©Ê´N¦n¹³¤@­ÓÅÜ¼Æ (Variable)¡A ¤£¦P¤§³B¦b©óÄݩʬO³sôµÛª«¥óªº¡C ÅܼƷ|¦b¤§«áªº½g³¹¸Ô­z¡C

²{¦b¤j®aÀ³¸Ó¹ï¤èªk©MÄݩʦ³ªì¨Bªº·§©À¤F¡A Åý§Ú¤¶²Ð¤@¤U±`¥Î document ªº¤èªk©MÄݩʧa¡G

Document ª«¥ó

¤èªk ÄÝ©Ê 
write() bgColor
writeln() fgColor
¡@ title
¡@ lastModified
¡@ URL

writeln()¡@©M¡@write()¡@ ªº¥\¯à¤@¼Ë¡A °ß¤@¤£¦Pªº¦a¤è´N¬O¡@write() ·|¦b¥y§À¥[¤JÂ_¦æ²Å¸¹ (Carrier Return)¡A ¥Øªº¬O¨Ï¼g¥X¨ÓªºªF¦è¦b´¶³q¤å¦r½s¿è¾¹¤¤ (¦p Notepad) ©ö©ó¾\Ū¡A ¤]¤è«K¦C¦L¡C ¦ý¦b¿Ã¹õ¤Wªº®ÄªG°ò¥»¤W¬O¤@¼Ëªº¡A ¦]¬° HTML ®æ¦¡¤ºªºÂ_¦æ²Å¸¹¤@¯ë·|³Q©¿²¤¡C

¨Ò¤l¡G document ªºÄÝ©Ê¡G




[/code]

°õ¦æ

µ{¦¡»¡©ú¡G

"The URL of this document is : " + document.URL + "
" ³o¬O write ªº¤@­Ó°Ñ¼Æ¡A ¦Ó³o­Ó°Ñ¼Æ¬O¤@­Ó¦r¦ê¡A ¥¦¦³¤T­Ó³¡¥÷¡A ¤À§O¬O "The URL of this document is : " ,¡@document.URL¡@©M¡@ "
"¡@¡A ¥¦­Ì¤§¶¡³£¦³­Ó¥[¸¹ (+)¡A ¬O¥Î¨Ó±N¨º¤T­Ó³¡¥÷²Õ¦X¦¨¤@­Ó§óªøªº¦r¦ê¡A µM«á³o­Ó¦r¦ê´N§@¬° write ªº°Ñ¼Æ¤F¡C

document.URL¡@ ¤£¥ÎÂù¤Þ¸¹¬AµÛ¡A¦]¬°§Ú­Ì­nÅã¥Ü¡@document.URL¡@ ªº¤º®e¡A ¦Ó¤£¬O¤@­Ó¡@document.URL¡@¦r¦ê¡C

document.write("The background color is " + document.bgColor + " and the text color is " + document.fgColor);

·í§Aµoı¤@¥y¦r¦ê¤Óªø®É¡A ¥i¥H¼g¦b¤U¤@¦æ¡A ¦Ó¤À³Îªº¦ì¸m¥i¥H¦b¥[¸¹ªº¥ª¥k¡A ¤À³Î¨ä¥¦¦ì¸m¥i¯à·|³y¦¨¿ù»~¡C

document.fgColor = "gold"¡@©M¡@document.bgColor = "black"
³o¨â¥y·|±N­I´ºÃC¦âÂন¶Â¦â¡A ±N¤å¦rÃC¦âÂনª÷¦â¡C ·Q§ïÅܪ«¥óÄݩʪº¤º®e¡A ¥u¶·§â¥¦·í§@ÅܼơA ¦A¥Îµ¥¸¹ (=) «ü©w¥¦ªº¤º®e«K¥i¡C


¦^ ¡P §Úªº js µ§°O»`¶° ³o¤@½g¤å³¹«Ê­±


¥»¤å±i¶KªÌ¡G®Õ®ÕÂF¡e±i¶K®É¶¡¡G¥Á°ê100¦~5¤ë24¤é¡]¬P´Á¤G¡^10ÂI01¤À¡f | ¼g«Hµ¹®Õ®ÕÂF

³¡¸¨®æ­º­¶


¾Ç²ßªº¬G¶m­º­¶
¥»¯¸¤½§i¡G¡e±z¶V»Ý­n§Ú­Ì¡A§Ú­Ì´N¶V¦³³Ð·N¡f ¥»¯¸»¡©ú®Ñ¡G¡eµo²{¬G¶mÁÙ¦³§ï¶iªº¦a¤è¡A½Ð¨Ó«H§i¶D­ì¤B­Ì¡f
Æ[¹îÀ³¥Î¾Ç²ßÂI¼Æ ¡G¡e«¥ªº¬G¶m¦³±zªº°Ñ»P¡A·|¨Ï§Ú­Ì¦³§ó¤jªºµo´§ªÅ¶¡¡A®i²{§óÂ×´Iºë±mªº¾Ç²ßµe­±¡f ¡e´Á«ÝÂǥѵL»Ùêºô­¶³]­p¡A¯àÅýµø»Ù¤pªB¤Í§ó·R¬Ý®Ñ¡B§ó·R¼g§@¥B§ó·R¾Ç²ß¡f¡Gª¼¥Î¹q¸£¡u¤ß±o¤À¨É¡v
¡e¬°¤FÅý§Ú­Ì¦³°®²bªº¾Ç²ßÀô¹Ò¡A½Ð¤Å¥ô·N¦b¥»¯¸´²¼½°Ó·~¼s§i»P¤£¦Xªk¤å¥ó©ÎÁpµ²¡f¡G¥»¯¸«Å¥Ü