How do I bind a click to an anchor without a framework (javascript)

前端 未结 6 594
难免孤独
难免孤独 2020-12-06 05:23

I know this is easily done in jQuery or any other framework, but that\'s not really the point. How do I go about \'properly\' binding a click event in pure javascript? I kno

6条回答
  •  太阳男子
    2020-12-06 05:40

    You don't have to use jQuery, but you could try John Resig's popular addEvent funciton.

    addevent(elem, "click",clickevent);  
    
    function addEvent ( obj, type, fn ) {
          if ( obj.attachEvent ) {
            obj["e"+type+fn] = fn;
            obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
            obj.attachEvent( "on"+type, obj[type+fn] );
          } else
            obj.addEventListener( type, fn, false );
        }
    

    There are more to be considered to'properly' bind an event on HTML tags in pure javascript.

    http://www.pagecolumn.com/javascript/bind_event_in_js_object.htm

提交回复
热议问题