我将如何使用 jQuery 显示与唯一链接最接近的段落元素?
标题有点生疏了,抱歉。
现在让我解释一下我想要做什么。
我有一些列出的项目,如下所示:
<li>
<a id="toggle" class="0"><h4>ämne<small>2010-04-17 kl 12:54</small></h4></a>
<p id="meddel" class="0">text</p>
</li>
<li class='odd'>
<a id="toggle" class="1"><h4>test<small>2010-04-17 kl 15:01</small></h4></a>
<p id="meddel" class="1">test meddelande :) [a]http://youtube.com[/a]</p>
</li>
我想要实现的功能是,当用户单击“切换”链接(h4 文本)时,我希望其下面的段落元素淡入。我想到了为切换链接和段落赋予相同的类,然后以某种方式使其获得与单击的切换链接具有相同类的段落并显示它的想法?但我也不完全确定如何做到这一点,而且说实话,这听起来不是最好的主意,但也许这是唯一的方法?我不知道...
是否有某种方法可以简单地获取ID为“meddel”的最近段落(链接下方)并将其淡入?这听起来容易一些……
我希望你至少能给我一些提示。 提前致谢, -耐克
The title is a bit rusty, sorry about that.
Now let me explain what i'm trying to do.
I have a few listed items, like this:
<li>
<a id="toggle" class="0"><h4>ämne<small>2010-04-17 kl 12:54</small></h4></a>
<p id="meddel" class="0">text</p>
</li>
<li class='odd'>
<a id="toggle" class="1"><h4>test<small>2010-04-17 kl 15:01</small></h4></a>
<p id="meddel" class="1">test meddelande :) [a]http://youtube.com[/a]</p>
</li>
The function i'm trying to achieve, is that when a user clicks a "toggle" link (the h4 text), i want the paragraph element below it to fade in. I thought of the idea of giving both the toggle link and the paragraph the same class, and then somehow make it get the paragraph with the same class as the toggle link clicked, and show it? But i'm not entirely sure how to do that either, and tbh, it doesn't sound like the greatest idea, but maybe that's the only way? I don't know...
Is there some way to just simply get the nearest paragraph (below the link) with the id "meddel" and fade it in? That sounds a bit easier...
I hope you can at least give me a few hints.
Thanks in advance,
-Nike
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 ID 需要是唯一的,我会给您的链接一个类,如下所示:
然后您可以执行以下操作:
在
的任何单击上都会找到下一个
并淡入淡出切换它。另外,课程不能以数字开头,请确保不要这样做以获得可预测的结果。
Your IDs need to be unique, I'd give your links a class, like this:
Then you can just do this:
On any click of
<a class="toggle">
this will find the next<p>
and fade toggle it. Also, classes can't start with a number, make sure to not do this for predictable results.问题之一:你们的锚点具有相同的 ID。他们不应该。 ID 应该是唯一的。
One issue: your anchors have the same ID. They shouldn't. IDs should be unique.
试试这个:
给你的链接一个
class
而不是id
。Try this:
Give your links a
class
instead ofid
.