【问题标题】:Add/remove class while traversing through list-items遍历列表项时添加/删除类
【发布时间】:2013-07-17 06:44:49
【问题描述】:

我不太明白为什么这不起作用。我正在努力做到.on()点击将removeClass().active-title从列表项中;然后addClass().active-titlenext() 列表项。

$(".next").on({
    click: function(){
        $('.active-title').removeClass(function(){
          $(this).next().addClass('active-title');
    }
  });
});

演示:

Link to JSFiddles Demo


Final Solution Demo in JSFIDDLES

【问题讨论】:

  • 点击JSFiddle中的“JSHint”按钮。

标签: javascript jquery onclick traversal


【解决方案1】:

您的代码中有一些错误

      }
   });
});

应该是

      }); <--- closing of removeClass
   } <-- closing for click function
});

试试这个

$(".next").on({
    click: function () {
       // Find the actuve element
        var $active = $('.active-title', 'ul');
       // If the next element exists the get the element
       // otherwise get the first li from the ul
        var $nextElem = $active.next('li').length ? $active.next('li') 
                                                  : $('ul').find('li:first');
        // Add and remove class
        $active.removeClass('active-title');
        $nextElem.addClass('active-title');

    }
});

Check Fiddle

【讨论】:

    猜你喜欢
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 2021-05-17
    • 2015-04-21
    相关资源
    最近更新 更多