中文(繁体)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http ://www.w3.org/1999/xhtml" lang="zh" xml:lang="zh"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF ; -8" /> <meta name="developer" content="Realazy" /> <title>JavaScript DOM 中的氣泡</title> <style type="text/css" media="screen"> div * { display:堵塞;邊距:4px;內邊距:4px;邊框:1px 純白色;} textarea {寬度:20em; height:2em;} </style> <script type="text/javascript"> //<![CDATA[ function init(){ var log = document.getElementsByTagName('textarea')[0]; var all = document.getElementsByTagName('div')[0].getElementsByTagName('*'); for (var i = 0, n = all.length; i < n; ++i){ all[i].onmouseover = function(e){ this.style.border = '1px 純紅色'; } log.value = '滑鼠現在進入的是: ' + this.nodeName; }; all[i].onmouseout = function(e){ this.style.border = '1px 純白色'; }; var all2 = document.getElementsByTagName('div')[1].getElementsByTagName('*'); for (var i = 0, n = all2.length; i < n; ++i){ all2[i].onmouseover = function(e){ this.style.border = '1px 純紅色'; } if (e) //停止事件冒泡 e.stopPropagation();否則 window.event.cancelBubble = true; log.value = '滑鼠現在進入的是: ' + this.nodeName; }; all2[i].onmouseout = function(e){ this.style.border = '1px 純白色'; }; } } window.onload = init; //]]> </script> </head> <body> <h1>JavaScript DOM 中的氣泡</h1> <p>DOM樹的結構是:</p> <pre><code> UL - LI - A - SPAN </code></pre> <div> <ul> <li><a href="#"><span>Bubbllllllllllllllle</span></a></li> <li><a href ="#"><span>Bubbllllllllllllllle</span></a></li> </ul> </div> <textarea></textarea> <p>滑鼠進入UL的任何一個子元素,如果不是停止冒泡,我們從UL到SPAN都定義了滑鼠懸停(<code>mouseover</code>)事件,這個事件會上升了UL,從而從滑鼠進入所的元素到UL元素都會有紅色的邊。 span >Bubbllllllllllllllllle</span></a></li> </ul> </div> <p>如果停止冒泡,事件不會上升,我們就可以獲得精確的滑鼠進入元素。 < /內文> </html>