javascript - Overriding default behaviour of anchor tags using jquery -


i trying override default behaviour of anchor tag can load in web page on server exisiting div, rather new tab or window.

so far have:

mycontainer.click(function(){                                        event.preventdefault();                     $('a').click(function(){                         var link = $(this).attr('href');                         mycontainer.load(link);                     });             }); 

in chrome have click link twice before anything, in ie ff doesnt work @ , refreshes page new link.

any appreciated.

shouldn't just:

 $('a').click(function(e) {      e.preventdefault();       mycontainer.load(this.href);  }); 

your code assigns click handler inside click handler. first click attach click handler link, , second click (on link) execute new click handler.

it seems need one click handler. if links added dynamically, can use .on() (the successor of .live , .delegate):

mycontainer.on('click', 'a', function(e) {      e.preventdefault();       mycontainer.load(this.href); });  // or  $(document).on('click', 'a', function(e) {      e.preventdefault();       mycontainer.load(this.href); }); 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -