我将如何使用 jQuery 显示与唯一链接最接近的段落元素?

发布于 2024-08-30 00:45:50 字数 778 浏览 2 评论 0原文

标题有点生疏了,抱歉。

现在让我解释一下我想要做什么。

我有一些列出的项目,如下所示:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的心境我的脸 2024-09-06 00:45:50

您的 ID 需要是唯一的,我会给您的链接一个类,如下所示:

<li>
    <a class="toggle"><h4>ämne<small>2010-04-17 kl 12:54</small></h4></a>
    <p>text</p>
</li>

<li class='odd'>
    <a class="toggle"><h4>test<small>2010-04-17 kl 15:01</small></h4></a>
    <p>test meddelande :) [a]http://youtube.com[/a]</p>
</li>

然后您可以执行以下操作:

$(function() {
  $("a.toggle").click(function() {
    $(this).next("p").animate({ opacity: 'toggle'});
  });
});

的任何单击上都会找到下一个

并淡入淡出切换它。另外,课程不能以数字开头,请确保不要这样做以获得可预测的结果。

Your IDs need to be unique, I'd give your links a class, like this:

<li>
    <a class="toggle"><h4>ämne<small>2010-04-17 kl 12:54</small></h4></a>
    <p>text</p>
</li>

<li class='odd'>
    <a class="toggle"><h4>test<small>2010-04-17 kl 15:01</small></h4></a>
    <p>test meddelande :) [a]http://youtube.com[/a]</p>
</li>

Then you can just do this:

$(function() {
  $("a.toggle").click(function() {
    $(this).next("p").animate({ opacity: 'toggle'});
  });
});

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.

我很OK 2024-09-06 00:45:50
$("h4").click(function() {
  var p = $(this).siblings("p");
  if (p.is(":hidden")) {
    p.fadeIn();
  } else {
    p.fadeOut();
  }
});

问题之一:你们的锚点具有相同的 ID。他们不应该。 ID 应该是唯一的。

$("h4").click(function() {
  var p = $(this).siblings("p");
  if (p.is(":hidden")) {
    p.fadeIn();
  } else {
    p.fadeOut();
  }
});

One issue: your anchors have the same ID. They shouldn't. IDs should be unique.

站稳脚跟 2024-09-06 00:45:50

试试这个:

$(function(){
    $('a.toggle, a.toggle h4').toggle(function(){
      $(this).next().fadeOut('slow');
    }, function(){
      $(this).next().fadeIn('slow');
    });
});

给你的链接一个class而不是id

Try this:

$(function(){
    $('a.toggle, a.toggle h4').toggle(function(){
      $(this).next().fadeOut('slow');
    }, function(){
      $(this).next().fadeIn('slow');
    });
});

Give your links a class instead of id.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文