jquery选择唯一的项目

我在选择html中的某个元素时遇到问题

当我点击带有“event_rsvp”类的链接时,我想在下一个li中使用“感兴趣的状态”类来实现跨度的HTML,我试过最近,尝试去父母,我怎么告诉一个人和另一个人说话。

  • Are You Going? – RSVP
  • Like It?
  • 重要提示这可能很重要,这是一个循环,我需要它只影响一个单击。

    您正在寻找处理jQuery的兄弟()选择器。 在这种特定情况下,您可能希望使用next()来检索LI的下一个匹配的兄弟。

     $('.rsvp_status').click(function() { // get the next LI with matching class $(this).parents().next('.interested-status'); }); 

    你可以通过导航来做到这一点。 链接位于列表元素中,您想要的范围位于下一个子节点中,因此顺序为:parent – > next – > child span。

     $("a.event_rsvp").click(function() { $(this).parent().next().children("span.interested-status").addClass('whatever"); return false; }); 
     $('a.event_rsvp').click(function() { $(this).parents('li:first').next().find('span:first').addClass('interested-status'); return false; }); 

    它可以通过获取父LI索引并将next()转换为一个查询的find()进行优化。