为你而来

javascript事件冒泡

 1 <html>
 2 <head>
 3 <style type='text/css'>
 4 div{
 5     border:dashed 1px green;
 6     width:200px;
 7     height:100px;
 8 }
 9 </style>
10 </head>
11 <body id='mybody'>
12 <div id='divone'>
13 <button id='clickme'>clickme</button>
14 </div>
15 <script language='javascript'>
16     obj('mybody').onclick=fun;
17     obj('clickme').onclick=fun;
18     obj('divone').onclick=fun;
19 
20     function obj(str){
21         if(typeof str=='string'){
22             return document.getElementById(str);
23         }
24     }
25     function fun(){
26         //alert(event.srcElement.id);//window.event event.target事件源
27         alert(this.id);//响应的对象
28     }
29 </script>
30 </body>
31 </html>

 

posted on 2012-06-03 21:19  为你而来  阅读(145)  评论(0编辑  收藏  举报

导航