0

我有一个带有自升式标记的常见问题解答,如下所示:

<p><strong>How can I come see the animals perform?</strong><br>
Schedules and information about attending a performance can be found here:</p>
<p><a href="http://jupmingdolphins.com">Performance tickets</a></p>
<p>If no performances are listed, it means that none are scheduled in the
near future. The animals take a break between November and May.</p>

<p><strong>What's the answer to this question?</strong><br>
It's 42, of course.</p>

<h2>Header for More Questions</h2>

<p><strong>Is it true the dolphins have smartphones?</strong><br>
Yes, they use Android phones and text each other constantly.</p>
<p><b>Just kidding!</b> They are all Apple fan-fish and prefer iPhones.</p>

(etc)

我试图弄清楚:

  1. 一些 CSS(可能还有 jQuery)在页面加载时隐藏除了问题之外的所有内容。

  2. 简单的 jQuery,当用户单击<strong>-wrapped 问题时,答案会向下滑动并出现在其下方。如您所见,问题在于标记很古怪(感谢 CMS),一个问题和另一个问题之间可能有很多东西。答案不包含在他们自己的 DIV 或任何东西中。最重要的是,FAQ 中有 H2 副标题,我不希望 H2 被触摸/折叠。

所以我需要类似于点击操作的代码:

$('strong').click(function() {
   // hide or reveal all elements from $(this) down,
   // and stop when we hit next <strong> or <h2>
});
4

1 回答 1

2

编辑

由于问题甚至没有自己的标签,这使情况变得非常复杂。因此,您处于需要隐藏问题的父标签但显示问题本身的尴尬境地,这基本上是不可能的。

为了解决这个问题,这是我整理的一个 hack 。它克隆问题并将它们附加到自己的标签中。但是,我建议尝试说服此标记的作者将其修改为更合理的内容。


要隐藏非问题,您似乎可以使用以下内容:

$("#container").children().not($("strong").parent()).not("p").hide();

要显示答案,您可以使用nextUntil

$(this).parent().nextUntil($("strong").parent()).show();

小提琴

于 2013-09-20T19:31:46.717 回答