javascript
JavaScript中的嵌套事件处理(在鼠标移动事件上)
Multiple event handling is the secret ingredient of dynamic WebPages seen now-a-days.
多重事件處理是當今動態網頁的秘密組成部分。
Now, let’s get started...
現在,讓我們開始吧...
Example Code
范例程式碼
<html lang="en"><head><meta charset="utf-8"><title>Pirates Booty</title><script>window.onload = init;function init() {var map = document.getElementById("map");map.onmousemove=showCoords;}function showCoords(eventObj) {var map = document.getElementById("coords");x=eventObj.clientX;y=eventObj.clientY;map.innerHTML = "Map coordinates: "+ x + ", " + y;}</script></head><body><img id="map" src="map.jpeg"><p id="coords">Move mouse to find coordinates...</p></body> </html>Output/Result
輸出/結果
On running the HTML file in the browser you will see that on moving the mouse over the image of the map, X and Y coordinates of the cursor get printed below the image.
在瀏覽器中運行HTML文件時,您會看到將鼠標移到地圖圖像上時,光標的X和Y坐標會打印在圖像下方。
NOW, let’s move on to the explanation part of the code.
現在,讓我們繼續進行代碼的解釋部分。
As usual HTML code is self explanatory.
通常,HTML代碼是不言自明的。
In the script tag, there are not many new things if you have gone through my previous article. The difference is that here I have used two event handlers instead of one and an event object.
在script標記中,如果您瀏覽了我的上一篇文章,則沒有太多新鮮事物。 區別在于,這里我使用了兩個事件處理程序,而不是一個事件對象。
EVENT OBJECT: Whenever an event occurs an event object corresponding to it is generated which has various details regarding the event like in case of on mouse move event its event object has the x & y coordinates of the point where mouse or cursor is placed.
事件對象:每當事件發生時,都會生成與事件相對應的事件對象,該事件對象具有有關事件的各種詳細信息,例如在鼠標移動事件的情況下,其事件對象具有放置鼠標或光標的點的x和y坐標。
First, init function has been assigned to onload event which occurs when Browser has fully generated the DOM of HTML code.
首先,已將init函數分配給onload事件,該事件在瀏覽器完全生成HTML代碼的DOM時發生。
In init function, it can be seen that another handler showcoords has been assigned to onmousemoveevent of the object map representing the image map.
在init函數中,可以看到已將另一個處理程序showcoords分配給表示圖像映射的對象映射的onmousemoveevent 。
Now, the major work zone i.e. the showcoords function.
現在,主要工作區域即showcoords功能。
As you can see event object corresponding to onmousemoveevent has been passed as an argument to showcoords handler.
如您所見,與onmousemoveevent對應的事件對象已作為參數傳遞給showcoords處理程序。
And then, clientX and clientY are properties of that eventobject which contain X & Y coordinate of cursor respectively.
然后, clientX和clientY是該事件 對象的屬性,分別包含光標的X和Y坐標。
These values are further getting printed via innerhtml property in paragraph element below the image.
這些值通過圖像下方段落元素中的innerhtml屬性進一步打印。
I have tried to explain each and every concept with utmost details.
我試圖用最詳盡的解釋來解釋每個概念。
Keep practicing till my next article.
繼續練習直到我的下一篇文章。
翻譯自: https://www.includehelp.com/code-snippets/nested-event-handling-in-javascript-on-mouse-move-event.aspx
總結
以上是生活随笔為你收集整理的JavaScript中的嵌套事件处理(在鼠标移动事件上)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 再见 Spring Task,这个定时任
- 下一篇: c# 赋值运算符_C#程序演示赋值运算符