jQuery 与 $(this).next 或 $(this).parent 的困境

发布于 2024-12-18 05:55:21 字数 1508 浏览 3 评论 0原文

使用:

$(document).ready(function(){
    $("a.toggle").removeClass('hide'); 
    $('p.extra_text').hide();
    $('a.toggle').click(function() {
        $(this).parent('div.bios').next('p.extra_text').slideToggle("slow", function(){ 
            $('a.toggle').text(function (index, text) {
                return (text == 'Read More . . .' ? 'Read Less . . .' : 'Read More . . .'); 
            });
        });    
        return false;
    });
});

with:

<div class="bios">      
  <p><strong>A Nobody</strong></p>
  <p>A Nobody live in Anywhere, USA.<a class="toggle hide">Read More . . .</a></p>
  <p class="extra_text">Wonder who A Nobody really is?  Well, we do too!</p>    

  <p><strong>A Nobody</strong></p>
  <p>A Nobody live in Anywhere, USA.<a class="toggle hide">Read More . . .</a></p>
  <p class="extra_text">Wonder who A Nobody really is?  Well, we do too!</p>    
 </div><!--/bios-->     

当点击任何a class="toggle"时,所有p class="extra_text"会滑动切换。如何将每个连续的 a class="toggle" 绑定到其自己的 p class="extra_text"

我已经看到使用 $(this).next$(this).parent 的其他答案,但它们失败了,因为在 p 类之前还有其他 p ="extra_text"。我怎样才能最好地选择正确的、连续的 a class="toggle" / p class="extra_text" 组合,而不用我的 Jquery 触发所有组合?

Using:

$(document).ready(function(){
    $("a.toggle").removeClass('hide'); 
    $('p.extra_text').hide();
    $('a.toggle').click(function() {
        $(this).parent('div.bios').next('p.extra_text').slideToggle("slow", function(){ 
            $('a.toggle').text(function (index, text) {
                return (text == 'Read More . . .' ? 'Read Less . . .' : 'Read More . . .'); 
            });
        });    
        return false;
    });
});

with:

<div class="bios">      
  <p><strong>A Nobody</strong></p>
  <p>A Nobody live in Anywhere, USA.<a class="toggle hide">Read More . . .</a></p>
  <p class="extra_text">Wonder who A Nobody really is?  Well, we do too!</p>    

  <p><strong>A Nobody</strong></p>
  <p>A Nobody live in Anywhere, USA.<a class="toggle hide">Read More . . .</a></p>
  <p class="extra_text">Wonder who A Nobody really is?  Well, we do too!</p>    
 </div><!--/bios-->     

When any a class="toggle" is clicked, all p class="extra_text" slideToggle. How to tie each successive a class="toggle" to its own p class="extra_text"?

I've seen other answers using $(this).next or $(this).parent, but they fail because there's that other p before the p class="extra_text". How may I best select the correct, successive a class="toggle" / p class="extra_text" combo without triggering all of 'em with my Jquery?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

一江春梦 2024-12-25 05:55:21

您需要 $(this).parent().next()

You want $(this).parent().next().

捎一片雪花 2024-12-25 05:55:21

正如 SLaks 指出的 $(this).parent().next() 是您所寻找的,但您的文本函数将更改每个类,而是这样做:

$('a.toggle').click(function() {
    var link = this;

    $(this).parent().next().slideToggle("slow", function() {
        $(link).text(function(index, text) {
            return (text == 'Read More . . .' ? 'Read Less . . .' : 'Read More . . .');
        });
    });
    return false;
});

Fiddle Demo: http://jsfiddle.net/KuQPP/

编辑

我通常不会走这么远来解释如何有些东西有效,但是在这种情况下,图片胜于雄辩,并且心情很慷慨,我创建了这个小图表,说明为什么您的代码不起作用,而这种方式可以。

说明图

在此处输入图像描述

As SLaks pointed out $(this).parent().next() is what your looking for but your text function will change every class, instead do this:

$('a.toggle').click(function() {
    var link = this;

    $(this).parent().next().slideToggle("slow", function() {
        $(link).text(function(index, text) {
            return (text == 'Read More . . .' ? 'Read Less . . .' : 'Read More . . .');
        });
    });
    return false;
});

Fiddle Demo: http://jsfiddle.net/KuQPP/

EDIT

I usually don't go this far to explain how something works but in this case pictures speak louder then words and was in a generous mood I created this little diagram of why your code doesn't work and this way does.

Explanation Diagram

enter image description here

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