简体   繁体   English

jQuery,向下滑动隐藏的div跳到顶部

[英]Jquery, sliding down hidden div jumps to the top

I have a link that once I click a hidden div slides down or up, but it always jumps to the top of the page so I have to go all the way down... 我有一个链接,一旦我单击一个隐藏的div就会上下滑动,但是它总是跳到页面顶部,因此我必须一直向下滑动...

I read I had to store a height but my content is dynamic so I can't do that... 我读到我必须存储一个高度,但是我的内容是动态的,所以我不能这样做...

Css 的CSS

.resultado{

    text-align: left;
}

Div itself Div本身

<div class="resultado">
<a class="mostrar" href="#">mostrar</a>
<div class="datos"><p>Hi</p></div>
</div>

Code

<script type="text/javascript">
    $(document).ready(function(){

        $(".datos").hide();

        $('.resultado').delegate('.mostrar','click',function(event){

          $(this).parent().find(".datos").slideToggle();

        }); 
    });
</script>   

Change: 更改:

$('.resultado').delegate('.mostrar','click',function(event){
    $(this).parent().find(".datos").slideToggle();
}); 

to

$('.resultado').delegate('.mostrar','click',function(event){
    event.preventDefault();
    $(this).parent().find(".datos").slideToggle();
}); 

The default behavior of your link is to follow the named anchor and bring the top of the page into focus. 链接的默认行为是跟随命名锚,并使页面顶部成为焦点。 By using event.preventDefault you suppress that behavior. 通过使用event.preventDefault您可以抑制该行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM