jquery - Detect if anchor is linked to an ID -


i wan smooth scroll when anchor clicked, first want check if anchor link linked id. if there hash don'nt scroll.

like:

<a href="#">do not scroll</a>  <a href="#anyid">yes scroll</a> 

my current code scroll on click every anchor have hash.

please fix code not scroll if anchor has hash

$('a[href^="#"]').click(function (e) {     e.preventdefault();     var target = this.hash;      if (typeof($(target).offset()) != 'undefined') {         $('html, body').animate({             scrolltop: $(target).offset().top - 60         }, 1000);     } }); 

if sure element exist each anchor not contain hash, change selector to:

$('a[href^="#"][href!="#"]') 

explicitly testing existence of element though.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -