开发者

slidetoggle, goes to top of page after selecting link

I have a div slide down when you click the link

<a href="#" id="related-courses">Related Courses</a>

<div id="related-courses-section">
 <p>Content</p>
</div>

$('#related-courses').click(funct开发者_如何学Goion() {
 $("#related-courses-section").slideToggle(100);
});

CSS set to display:none; for #related-courses-section (to hide it by default)

It will display the div w/ the content within it, but it brings me to the top of the page after clicking the link, why would it do that?


Try adding return false; to your click function, like so:

$('#related-courses').click(function() {
 $("#related-courses-section").slideToggle(100);
 return false;
});

This will prevent the default link click behavior of navigating to the link url.


preventDefault() is a more flexible approach - it's functionally the same as return false, but can be placed at the beginning of the function and doesn't preclude 'return'ing a value for some other reason.

$('#related-courses').click(function(e) {
  e.preventDefault();
  $("#related-courses-section").slideToggle(100);
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜