معرفی رویدادهای عناصر HTML

اسناد HTML این توانایی را دارند که با هر عملی یک عکس‌العمل را اجرا کند.

در واقع در HTML به رویدادها و اتفاقات مشخصی که توسط کاربر یا به صورت خودکار رخ می‌دهد، اجازه می‌دهد که با وقوع آن رخداد اقداماتی را در مرورگر انجام دهد. مانند شروع یک جاوا اسکریپت زمانی که کاربر روی یک تگ HTML کلیک می کند. برای کسب اطلاعات بیشتر در مورد رویدادهای برنامه نویسی، لطفاً از آموزش جاوا اسکریپت ما دیدن کنید. در زیر ویژگی‌های رویداد‌های عمومی وجود دارد که می‌توانند به عناصر HTML برای انجام اقداماتی تعریف شوند.

ویژگی‌های رویدادهای صفحه مرورگر (Window Event Attributes)

رویدادهای فعال شده برای پنجره مرورگر وب بر روی تگ <body> اعمال می‌شوند:

ویژگی تگمقدارتوضیح
onafterprintscriptScript to be run after the document is printed
onbeforeprintscriptScript to be run before the document is printed
onbeforeunloadscriptScript to be run when the document is about to be unloaded
onerrorscriptScript to be run when an error occurs
onhashchangescriptScript to be run when there has been changes to the anchor part of the a URL
onloadscriptFires after the page is finished loading
onmessagescriptScript to be run when the message is triggered
onofflinescriptScript to be run when the browser starts to work offline
ononlinescriptScript to be run when the browser starts to work online
onpagehidescriptScript to be run when a user navigates away from a page
onpageshowscriptScript to be run when a user navigates to a page
onpopstatescriptScript to be run when the window's history changes
onresizescriptFires when the browser window is resized
onstoragescriptScript to be run when a Web Storage area is updated
onunloadscriptFires once a page has unloaded (or the browser window has been closed)

رویدادهای فرم (Form Events)

رویدادهایی که توسط فیلدهای داخل یک فرم HTML ایجاد می‌شوند، تقریباً برای همه عناصر HTML نیز اعمال می‌شوند، ولی بیشتر در عناصر فرم استفاده می‌شوند:

ویژگی تگمقدارتوضیح
onblurscriptFires the moment that the element loses focus
onchangescriptFires the moment when the value of the element is changed
oncontextmenuscriptScript to be run when a context menu is triggered
onfocusscriptFires the moment when the element gets focus
oninputscriptScript to be run when an element gets user input
oninvalidscriptScript to be run when an element is invalid
onresetscriptFires when the Reset button in a form is clicked
onsearchscriptFires when the user writes something in a search field (for <input="search">)
onselectscriptFires after some text has been selected in an element
onsubmitscriptFires when a form is submitted

رویدادهای صفحه کلید (Keyboard Events)

ویژگی تگمقدارتوضیح
onkeydownscriptFires when a user is pressing a key
onkeypressscriptFires when a user presses a key
onkeyupscriptFires when a user releases a key

ویدادهای عملکردهای ماوس (Mouse Events)

ویژگی تگمقدارتوضیح
onclickscriptFires on a mouse click on the element
ondblclickscriptFires on a mouse double-click on the element
onmousedownscriptFires when a mouse button is pressed down on an element
onmousemovescriptFires when the mouse pointer is moving while it is over an element
onmouseoutscriptFires when the mouse pointer moves out of an element
onmouseoverscriptFires when the mouse pointer moves over an element
onmouseupscriptFires when a mouse button is released over an element
onmousewheelscriptDeprecated. Use the onwheel attribute instead
onwheelscriptFires when the mouse wheel rolls up or down over an element

رویدادهای کشیدن عناصر (Drag Events)

ویژگی تگمقدارتوضیح
ondragscriptScript to be run when an element is dragged
ondragendscriptScript to be run at the end of a drag operation
ondragenterscriptScript to be run when an element has been dragged to a valid drop target
ondragleavescriptScript to be run when an element leaves a valid drop target
ondragoverscriptScript to be run when an element is being dragged over a valid drop target
ondragstartscriptScript to be run at the start of a drag operation
ondropscriptScript to be run when dragged element is being dropped
onscrollscriptScript to be run when an element's scrollbar is being scrolled

رویدادهای عملیات حافظه (Clipboard Events)

ویژگی تگمقدارتوضیح
oncopyscriptFires when the user copies the content of an element
oncutscriptFires when the user cuts the content of an element
onpastescriptFires when the user pastes some content in an element

رویدادهای رسانه (Media Events)

رویدادهایی که توسط رسانه‌هایی مانند ویدیوها، تصاویر و صدا ایجاد می‌شوند. 

این نوع رویدادها برای همه تگ‌های HTML اعمال می‌شود، ولی در عناصر رسانه مانند <audio>، <embed>، <img>، <object> و <video> رایج‌تر هستند.

نکته: برای اطلاعات بیشتر به صفحه  مرجع DOM صوتی و تصویری HTML مراجعه کنید.

ویژگی تگمقدارتوضیح
onabortscriptScript to be run on abort
oncanplayscriptScript to be run when a file is ready to start playing (when it has buffered enough to begin)
oncanplaythroughscriptScript to be run when a file can be played all the way to the end without pausing for buffering
oncuechangescriptScript to be run when the cue changes in a <track> element
ondurationchangescriptScript to be run when the length of the media changes
onemptiedscriptScript to be run when something bad happens and the file is suddenly unavailable (like unexpectedly disconnects)
onendedscriptScript to be run when the media has reach the end (a useful event for messages like "thanks for listening")
onerrorscriptScript to be run when an error occurs when the file is being loaded
onloadeddatascriptScript to be run when media data is loaded
onloadedmetadatascriptScript to be run when meta data (like dimensions and duration) are loaded
onloadstartscriptScript to be run just as the file begins to load before anything is actually loaded
onpausescriptScript to be run when the media is paused either by the user or programmatically
onplayscriptScript to be run when the media is ready to start playing
onplayingscriptScript to be run when the media actually has started playing
onprogressscriptScript to be run when the browser is in the process of getting the media data
onratechangescriptScript to be run each time the playback rate changes (like when a user switches to a slow motion or fast forward mode)
onseekedscriptScript to be run when the seeking attribute is set to false indicating that seeking has ended
onseekingscriptScript to be run when the seeking attribute is set to true indicating that seeking is active
onstalledscriptScript to be run when the browser is unable to fetch the media data for whatever reason
onsuspendscriptScript to be run when fetching the media data is stopped before it is completely loaded for whatever reason
ontimeupdatescriptScript to be run when the playing position has changed (like when the user fast forwards to a different point in the media)
onvolumechangescriptScript to be run each time the volume is changed which (includes setting the volume to "mute")
onwaitingscriptScript to be run when the media has paused but is expected to resume (like when the media pauses to buffer more data)

سایر رویدادها (Misc Events)

ویژگی تگمقدارتوضیح
ontogglescriptFires when the user opens or closes the <details> element

این صفحه را به دوستان خود معرفی کنید

با به اشتراک گذاری این صفحه، یک فرصت خوب به دوستان خود هدیه می‌دهید