1

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

当我点击“event_rsvp”类的链接时,我想用我尝试过的“interested-status”类来影响下一个li中span的HTML,试着去父母那里,我怎么知道一个与另一个交谈。

<li rel="101590"><span class="rsvp-status">Are You Going? –&amp;nbsp;<a href="javascript:;" class="event_rsvp" rel="attending">RSVP</a></span></li>

<li rel="101590"><span class="interested-status"> <a href="javascript:;" class="event_likeit" rel="likeit"> Like It?</a></span></li>

重要 提到这是一个循环可能很重要,我需要它只影响单击的那个。

4

3 回答 3

3

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

$('.rsvp_status').click(function() {
    // get the next LI with matching class
    $(this).parents().next('.interested-status');
});
于 2009-11-09T00:54:56.687 回答
1

您可以通过导航来完成。该链接位于列表元素中,您想要的跨度位于下一个子元素中,因此顺序为:父 -> 下一个 -> 子跨度。

$("a.event_rsvp").click(function() {
  $(this).parent().next().children("span.interested-status").addClass('whatever");
  return false;
});
于 2009-11-09T00:41:29.790 回答
0
$('a.event_rsvp').click(function() {
  $(this).parents('li:first').next().find('span:first').addClass('interested-status');
  return false;
});

可以通过将父 LI 索引和边距 next() 放入 find() 来进行优化,以进行一个查询。

于 2009-11-09T00:58:56.753 回答