【问题标题】:jquery on toggle only change the surrounding divs classjquery on toggle 仅更改周围的 div 类
【发布时间】:2011-05-24 21:38:02
【问题描述】:

当我点击隐藏信息链接时,它会显示显示信息,但它也会更改页面上所有其他框上的文本,当我点击隐藏信息它会切换 div class="revealBox" 上的 test 类,但它会在所有框上执行此操作,我想将其保留在相关框上。

我知道这将与 $(this) 有关,我只是不知道如何实现它。

这是我的 jquery

$(document).ready(function() {
// choose text for the show/hide link - can contain HTML (e.g. an image)
var showText='Hide Information';
var hideText='Show Information';
var is_visible = false;
$('.collapseLink').append('<span class="dottedBot">'+showText+'</span>');
$('.revealBoxContents').show();
$('a.collapseLink').click(function() {
// switch visibility
is_visible = !is_visible;
// change the link depending on whether the element is shown or hidden
$(this).html( (!is_visible) ? showText : hideText);
// toggle the display - uncomment the next line for a basic "accordion" style
//$('.toggle').hide();$('a.toggleLink').html(showText);
$(this).parent().next('.revealBoxContents').slideToggle('slow');
// return false so any link destination is not followed
return false;
});
// toggle the bottom link
$('.collapseLink').click(function(){
     $(this).parents('.revealBoxContents').stop(true, true).slideToggle('slow');    
     $(".collapseLink").html( (!is_visible) ? showText : hideText);
    $(this).parent('.item').toggleClass('current');

}); 

$(".revealBoxTop a").click(function(){
        $(this).toggleClass("closed").next().slideToggle("slow");
        return false; //Prevent the browser jump to the link anchor
    });

    $('.revealBox a').click(function() {
        $(".revealBox").toggleClass("test");
    });


});

这是网址

http://satbulsara.com/NSJ-local/eqs1.htm

【问题讨论】:

  • 您可以使用.is(':visible') 测试可见性。
  • 您使用is_visible 变量并更改其值。只需在 if 语句中使用上述属性 $(this).is(':visible')

标签: jquery


【解决方案1】:

编辑

好的,我又试了一次 - 在没有测试的情况下改变了很多!请用下面的代码替换您的代码并提供建议。如果这不起作用,最好在http://www.jsbin.comhttp://www.jsfiddle.net 上设置演示

// choose text for the show/hide link - can contain HTML (e.g. an image)
var showText='Hide Information',
    hideText='Show Information';

$('.collapseLink').append('<span class="dottedBot">'+showText+'</span>');
$('.revealBoxContents').show();

// toggle the bottom link
$('.revealBox .collapseLink').click(function(e){
         e.preventDefault();
         var $targetContainer = $(this).parents('.revealBox').find('revealBoxContents');
         $targetContainer.stop(true, true).slideToggle('slow');
         if ($targetContainer.is(:visible) {
             $(this).html(showText);
         else {
             $(this).html(hideText);
         }
         // this line below doesn't do anything - can't find an element .item
         $(this).parent('.item').toggleClass('current');
}); 

$('.revealBox a').click(function() {
    // ONLY TARGET PARENT REVEAL BOX
    $(this).parents(".revealBox").toggleClass("test");
});

希望我们越来越近了!

编辑 3

继续使用您的示例 html 制作模型: http://jsbin.com/iceyi3/edit 编辑: 修改版本:http://jsbin.com/iceyi3/2/edit

我在上面的代码中发现了一些粗心的错误 - 所以我为此道歉! “隐藏文本”/“显示文本”的重复是因为在某些区域它们存在于 html 中以及由 javascript 添加。

【讨论】:

  • 这似乎不起作用,因为它不会影响 div .revealBox,而且它对文本更改做了一些奇怪的事情,因为我需要让底部链接保持独立
  • 用一些想法编辑了上面的内容。请告知“我需要让底部链接独立”是什么意思
  • 是的,当你点击它时,底部的链接会改变手风琴的流程,如果你点击它,当盒子已经打开时,它会在顶部的文本中显示显示信息跨度>
  • 抱歉,有点困惑。您是否看过上面的 jsbin 示例(在编辑 3 下的回答)。我在那里所做的事情是,点击区域底部的隐藏/显示链接会触发该区域顶部的隐藏/显示链接上的点击事件。因此手风琴的功能没有区别。此外,当手风琴关闭时,您将无法再看到底部的隐藏/显示链接。对不起,如果我误解你了!!
  • 您好,感谢您的帮助,它几乎就在那里,但是如果您查看 js bin 并首先单击底部链接,顶部链接仍然显示隐藏信息,当信息已经隐藏,然后当您单击顶部隐藏信息链接随后展开底部链接时显示信息已经显示时显示信息。我想知道单击任何一个时如何保持文本的逻辑。谢谢
猜你喜欢
  • 2013-02-26
  • 2018-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-09
  • 1970-01-01
相关资源
最近更新 更多