javascript - How to use e.preventDefault() on $(this) inside the $(window).resize()? -
how can use $(this).e.preventdefault()
?
what want / need on window resize <a>
should behave differently. in fiddle example if window width smaller 710 href
should not triggered. there javascript function toggling subnav
. if window wider 710px, want href
work "normally".
now setting example i've seen event firing multiple times. how can if fires once?
also, there better way achieve want?
please resize window in jsfiddle before clicking! fiddle here
$( window ).resize(function() { var ww = $(window).width(); $('.windowwidth span').html(ww); if(ww < 710) { $('nav ul.nav > li > a').on('click',function(e){ e.preventdefault(); alert("here"); }); } else { $('nav ul.nav > li > a').on('click',function(e){ alert("there"); }); } });
you need check window width within click handler instead of setting click handler in window resize handler. try this:
$('nav ul.nav > li > a').on('click',function(e){ if ($(window).width() < 710) { e.preventdefault(); } alert("there"); });
your current method not work end assigning same click handler same element multiple times.
Comments
Post a Comment