There are two ways of event propagation in the HTML DOM: bubbling and capturing.
在HTML DOM裡面有兩個事件傳播的方式:冒泡(bubbling)和捕捉(capturing)。
Event propagation allows for the definition of the element order when an event occurs. If you have a <p> element inside a <div> element, and the user clicks on the <p> element, which element's "click" event should be handled first?
In bubbling, the innermost element's event is handled first and then the outer element's event is handled. The <p> element's click event is handled first, followed by the <div> element's click event.
In capturing, the outermost element's event is handled first and then the inner. The <div> element's click event is handled first, followed by the <p> element's click event.
TIP:This is particularly useful when you have the same event handled for multiple elements in the DOM hierarchy.
-----
Creating an Image Slider
新增輪播圖
Now we can create a sample image slider project. The images will be changed using "Next" and "Prev" buttons. Now, let's create our HTML, which includes an image and the two navigation buttons:
var images = [ "http://www.sololearn.com/uploads/slider/1.jpg", "http://www.sololearn.com/uploads/slider/2.jpg", "http://www.sololearn.com/uploads/slider/3.jpg" ]; var num = 0;
function next() { var slider = document.getElementById("slider"); num++; if(num >= images.length) { num = 0; } slider.src = images[num]; }
function prev() { var slider = document.getElementById("slider"); num--; if(num < 0) { num = images.length-1; } slider.src = images[num]; }
The num variable holds the current image. The next and previous button clicks are handled by their corresponding functions, which change the source of the image to the next/previous image in the array.
翻譯小心得| capturing和bubbling也是接觸過,但沒有常用的概念,有明確的定義和簡單的範例說明後,可以很清楚掌握。 第二個部分的輪播圖操作,概念是學過、用過,但之前沒有做類似功能的小作品,也是不錯的收穫。 Finally, the article above is from SOLOLEARN . I translated it just for practice, personal use only. If that is inappropriate, please contact me.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.