Function work after second click

前端 未结 2 1935
Happy的楠姐
Happy的楠姐 2021-01-29 11:14

Anyboby help. Why function add class active this.parents(\".block-parent\").find(\".periods[data-period=\"+typelink+\"]\").addClass(\'active\') work after second cl

2条回答
  •  野的像风
    2021-01-29 11:54

    because in first click you initialize the code so it works in second click! common logic use $(function()); like this

    $(function(){
        var $eachblocks = $(".top10_month .periods");
    var $blockhead = $(".block-head__link");
    $blockhead.on("click", function(e){
    
        var $this = $(this);
        var typelink = $(".block-head__link.active").attr("data-date");
        e.preventDefault();
        $this.parents("ul").find("a").removeClass("active");
        this.className += " active";
        $this.parents(".block-parent").find(".periods").removeClass('active');
        $this.parents(".block-parent").find(".periods[data-period="+typelink+"]").addClass('active');
    });
    
    });
    

    try this this should work

提交回复
热议问题