链接不响应click事件

时间:2016-11-26 12:42:49

标签: jquery

添加以下脚本后,链接(如 <a href="http://www.example.com">example.com</a>停止了工作。他们不再响应点击事件了。

有人可以帮我解决这个问题吗?

$(document).ready(
function() {
    $(".show").click(function() {
        $(".show").hide();
        $(".hide").show();
        $("#cpks-table").fadeIn();
    });
});

$(document).ready(
function() {
     $(".hide").click(function() {
        $(".show").show();
        $(".hide").hide();
        $("#cpks-table").fadeOut();
    });

    $(".hide").click(function(){
    $('html, body').animate({scrollTop : 0},800);
    return false;
    });

    $( ".tggl" ).click(function() {
         $(".show").fadeToggle();
        $(".hide").fadeToggle();
    $( "#cpks-table" ).fadeToggle();
    }); 

$(document).on('click', 'a', function(event){
event.preventDefault();

$('html, body').animate({
    scrollTop: $( $.attr(this, 'href') ).offset().top-100
}, 500);
});

});

2 个答案:

答案 0 :(得分:1)

您好,您正在通过

阻止链接的默认行为
event.preventDefault();

线。动画完成后,您应该通过Javascript打开链接,

window.open($.attr(this, 'href'))

所以你应该写点像

$(document).on('click', 'a', function(event){
    event.preventDefault(); //preventing default behavior of link click
    //do what you need
    var self = this;
    $('html, body').animate({
        scrollTop: $( $.attr(self, 'href') ).offset().top-100
    }, 500, function() {
        //redirect user where intended
        window.location.href = $.attr(self, 'href');
    });
});

答案 1 :(得分:0)

您需要更多行:

 $(document).on('click', 'a', function(event){

    event.preventDefault(); //preventing default behavior of link click
    //do what you need
    $('html, body').animate({
        scrollTop: $( $.attr(this, 'href') ).offset().top-100
    }, 500);
   //redirect user where intended
    window.location.href=$(this).attr('href');

    });
相关问题