Javascript event bubble demo

Source: Internet
Author: User

  1. <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en"
  2. Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
  3. <HTML xmlns = "http://www.w3.org/1999/xhtml" lang = "ZH" XML: lang = "ZH">
  4. <Head>
  5. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
  6. <Meta name = "Developer" content = "realazy"/>
  7. <Title> bubble in Javascript DOM </title>
  8. <Style type = "text/CSS" Media = "screen">
  9. Div * {display: block; margin: 4px; padding: 4px; Border: 1px solid white ;}
  10. Textarea {width: 20em; Height: 2em ;}
  11. </Style>
  12. <SCRIPT type = "text/JavaScript">
  13. // <! [CDATA [
  14. Function Init (){
  15. VaR log = Document. getelementsbytagname ('textarea ') [0];
  16. VaR all = Document. getelementsbytagname ('div ') [0]. getelementsbytagname ('*');
  17. For (VAR I = 0, n = all. length; I <n; ++ I ){
  18. All [I]. onmouseover = function (e ){
  19. This. style. Border = '1px solid red ';
  20. Log. value = 'the cursor enters: '+ this. nodename;
  21. };
  22. All [I]. onmouseout = function (e ){
  23. This. style. Border = '1px solid white ';
  24. };
  25. }
  26. VaR all2 = Document. getelementsbytagname ('div ') [1]. getelementsbytagname ('*');
  27. For (VAR I = 0, n = all2.length; I <n; ++ I ){
  28. All2 [I]. onmouseover = function (e ){
  29. This. style. Border = '1px solid red ';
  30. If (e) // stop event bubbling
  31. E. stoppropagation ();
  32. Else
  33. Window. event. cancelbubble = true;
  34. Log. value = 'the cursor enters: '+ this. nodename;
  35. };
  36. All2 [I]. onmouseout = function (e ){
  37. This. style. Border = '1px solid white ';
  38. };
  39. }
  40. }
  41. Window. onload = Init;
  42. //]>
  43. </SCRIPT>
  44. </Head>
  45. <Body>
  46. <H1> bubble in Javascript DOM
  47. <P> the DOM tree structure is: </P>
  48. <PRE> <code>
  49. Ul
  50. -Li
  51. -
  52. -Span
  53. </Code> </PRE>
  54. <Div>
  55. <Ul>
  56. <Li> <a href = "#"> <span> bubbllllllllllllllle </span> </a> </LI>
  57. <Li> <a href = "#"> <span> bubbllllllllllllllle </span> </a> </LI>
  58. </Ul>
  59. </Div>
  60. <Textarea> </textarea>
  61. <P> move the cursor to any sub-element of UL. If the bubble is not stopped, the <code> Mouseover </code> event is defined from ul to span, this event will rise to ul, so that the elements entering the mouse will have a red edge to the UL element. </P>
  62. <Div>
  63. <Ul>
  64. <Li> <a href = "#"> <span> bubbllllllllllllllle </span> </a> </LI>
  65. <Li> <a href = "#"> <span> bubbllllllllllllllle </span> </a> </LI>
  66. </Ul>
  67. </Div>
  68. <P> if you stop bubbling and the event does not rise, you can obtain the precise mouse entry element. </P>
  69. </Body>
  70. </Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.