【问题标题】:jQuery -- adding and removing a class using next() across paragraphsjQuery - 使用 next() 跨段落添加和删除类
【发布时间】:2011-09-11 05:13:38
【问题描述】:

我有一个文档,其中包含各种跨度,其中包含 .highlight 类。第一个选定的项目上也有 .currentItem,表示它是当前选定的项目。当用户单击按钮时,我希望能够浏览到下一个突出显示的项目。这是我能说的最好的功能:

function goNextHighlight() {
    var $active = $('.currentItem');
    var $next = $('.currentItem').next('.highlight');

    $active.removeClass('currentItem');
    $next.addClass('currentItem');
}

$active 已正确设置,并且正在从中删除 currentItem 类。但是,$next 通常不起作用,因为下一个突出显示的项目在另一个 div 或段落中。 Here's a jsfiddle that shows the problem。如果您单击下一个按钮两次,您会看到它第一次有效,但第二次无效。

无论它在文档中的什么位置,我如何使它工作,以便它转到下一个匹配的 .highlight?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:
    var $next = $('.highlight').eq($active.index('.highlight')+1);
    

    示例:http://jsfiddle.net/Paulpro/qjsjt/1/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 2011-03-24
      • 1970-01-01
      • 1970-01-01
      • 2013-10-31
      • 2015-06-28
      相关资源
      最近更新 更多