Function work after second click

前端 未结 2 1937
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:53

    It's because you're using this

    var typelink = $(".block-head__link.active").attr("data-date")
    

    to find the link you've just clicked on, and you haven't dealt with changing the classes yet so it's getting the previous element you gave the class active to. The following will return the correct data-date value of the clicked element, not one you think it might be that's saught by class name.

    var typelink = $(this).data("date");
    // var typelink = $(this).attr("data-date"); // this isn't how data is used
    

    http://jsfiddle.net/kngU8/7/

提交回复
热议问题