Bootstrap模式中的链接无法按预期工作

时间:2016-12-22 23:14:41

标签: javascript jquery css twitter-bootstrap bootstrap-modal

我在模态中有一个链接,我想在点击后将用户带到特定位置。目前,这是第一次使用,但在使用"关闭"时使用相同的模态。模式的按钮(不是特定链接),它也将我发送到我的链接(不是预期的行为)。

这是一个js小提琴,它显示了Bootstrap模式中链接的奇怪行为。 https://jsfiddle.net/x9kr2wwm/5/

我的尝试是对此处发现的非常相似的问题/答案(但不是重复)的修改:CSS Bootstrap close modal and go to link

HTML:

See the <a href="#getstarted" id="gotoLink" data-dismiss="modal">"Get Started"</a> section for more

javascipt的:

jQuery(function($) {
            $("a#gotoLink").click(function(){
                $('#portfolioModal1').on('hidden.bs.modal', function (e) {
                    $('html, body').animate({
                        scrollTop: $("#getstarted").offset().top
                    }, 2000);
                })
           });
    });

1 个答案:

答案 0 :(得分:1)

<a>元素的默认行为是将页面重定向到其href属性。要防止默认行为,请使用jQuery的.preventDefault()方法。

jQuery(function($) {
            $("a#gotoLink").click(function(event){
                event.preventDefault(); 

                $('#portfolioModal1').on('hidden.bs.modal', function (e) {
                    $('html, body').animate({
                        scrollTop: $("#getstarted").offset().top
                    }, 2000);
                })
           });
    });