javascript绑定事件,JQuery绑定事件

javascript绑定按钮事件,JQuery绑定按钮事件,其他事件类似

 

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" >
< head >
< script  type ="text/javascript"  src ="jquery-1.4.2.min.js" > script >
head >

< body >
< button  id ="btn1" > 原生Javascript绑定 button >< br />
< button  id ="btn2" > JQeury绑定1 button >< br />
< button  id ="btn3" > JQeury绑定2 button >< br />
< script  type ="text/javascript" >
  $(document).ready(
function (){
      
      $(
" #btn2 " ).click( function (){
           alert(
" JQuery绑定1 " );
      });

      $(
" #btn3 " ).bind( " click " , function (){
          alert(
" JQuery绑定2 " );
      });
  });

  
var  bind = function (object,type,fn){
      
if (object.attachEvent){
            
// IE浏览器
            object.attachEvent( " on " + type,( function (){ return   function (){window.event.cancelBubble = true ;object.attachEvent = fn.apply(object);}})(object));
      }
else   if (object.addEventListener){
            
// 其他浏览器
            object.addEventListener(type, function (event){event.stopPropagation();fn.apply( this )}, false );
      }   
  }
  bind(document.getElementById(
" btn1 " ), " click " , function (){alert( " 点击按钮的ID  " + this .id + "  ! " )});
script >
body >
html >

-

你可能感兴趣的:(javascript绑定事件,JQuery绑定事件)