【问题标题】:jquery hide/show divs troubleshootingjquery 隐藏/显示 div 疑难解答
【发布时间】:2014-12-29 20:25:02
【问题描述】:

我有一系列链接来隐藏/显示六个不同的 div。我希望页面加载隐藏所有六个 div 的位置。单击六个链接之一时,相应的 div 应淡入。单击任何其他链接时,当前应淡出,并应出现新选择的 div。

这是我放在正文开头标记内的 jQuery 脚本的开头:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>
    $("#1").on('click', function() {
   $(this).fadeOut();
   $("#Graphics, #Jury, #Witness, #Mock, #Continuing").fadeOut();
   $("#Strategy").fadeIn();
});
    </script>

以下是链接:

<a href="#Strategy" id="1">Strategy &amp; Theme Development</a>
    <a href="#Graphics" id="2">Graphics</a>
    <a href="#Jury" id="3">Jury Selection</a>
    <a href="#Witness" id="4">Witness Preparation</a>
    <a href="#Mock" id="5">Mock Trials &amp; Focus Groups</a>
    <a href="#Continuing" id="6">Continuing Legal Education</a>

这些是目标 div:

<div id="Strategy"></div>
<div id="Graphics"></div>
<div id="Jury"></div>
<div id="Witness"></div>
<div id="Mock"></div>
<div id="Continuing"></div>

我不确定 div 的链接是否会覆盖 jQuery 效果。

【问题讨论】:

  • 如果 div 最初是隐藏的,为什么要淡出它们?
  • 我的目标是在点击第二个链接后将它们淡出。目前我没有隐藏 div,只是想解决问题。

标签: javascript jquery html css ajax


【解决方案1】:

我相信您正在寻找这样的东西:

$(document).ready(function() {
    $("a").on('click', function (e) {
        var currentElement = $(e.currentTarget);
        var divToShow = currentElement.attr('href'); // grab the id of the div we need to show
        // now find an visible divs
        var visibleDiv = $('div').filter(function() { return $(this).is(':visible'); });

        if (visibleDiv.length) // if there is a div already visible, fade it out then fade in the target div
        {
            $(visibleDiv).fadeOut(400, function() {
                $(divToShow).fadeIn();
            });
        }
        else $(divToShow).fadeIn(); // fade it in.
    });
});

您已经将要淡入淡出的divs 的ID 通过其href 属性链接到a 标签。

我只需在a 标记中添加一个通用类来控制divs 的淡入淡出,然后当点击其中任何一个时,抓住它的href 属性并淡入该div,同时淡出当前可见的 div(如果存在)。

我还将向可褪色的divs 添加一个通用类,这样您就可以在不触及 DOM 的其余部分的情况下过滤它们。

JSFiddle Here

更新:

以下是为元素提供通用类的示例:

对于a 标签,将hideShow 类添加到所有标签:

<a href="#Strategy" id="1" class="hideShow">Strategy &amp; Theme Development</a>
<a href="#Graphics" id="2" class="hideShow">Graphics</a>
<a href="#Jury" id="3" class="hideShow">Jury Selection</a>
etc...

对于“div”标签,将hideable类添加到所有标签中:

<div id="Strategy" class="hideable">The strategy div</div>
<div id="Graphics" class="hideable">The Graphics div</div>
<div id="Jury" class="hideable">The Jury div</div>
etc...

然后在jQuery中:

$(document).ready(function() {
    $(".hideShow").on('click', function (e) {
        // you clicked one of the `.hideShow` a tags!
        ....
        // now filter through all the divs with `class="hideable"` to find a visible one
        var visibleDiv = $('.hideable').filter(function() { return $(this).is(':visible'); });
    });
});

【讨论】:

  • 谢谢,我试试看。
  • 关于分配普通类,你能举个例子吗?脚本编写不太熟练,我很难理解它是如何工作的。
  • @JoeO'Toole 查看我帖子中的更新。希望对您有所帮助。
  • 非常感谢。我试试看。
  • 这很完美!后续问题:如何在页面加载时隐藏所有这些 div?
【解决方案2】:

您应该选择元素的 ID,而不是元素的 href 值。

$("#1").on('click', function() {
$(this).fadeOut();
$("#2, #3, #4, #5, #6").fadeOut();
$("#1").fadeIn();
});

【讨论】:

  • 不,这些是链接 id (#1-#6),我要显示/隐藏的 div id 是#Strategy、#Graphics 等。
  • 您还应该包含 div 元素的代码以避免混淆。
  • 好的,我已经添加了目标 div。对此感到抱歉。
【解决方案3】:

您可能想尝试单独对它们采取行动:

$("#Graphics,#Jury,#Witness,#Mock,#Continuing").each(function(){ $(this).fadeOut(); });

你可能还想看看.toggle()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    • 2018-09-06
    • 2014-08-29
    • 2011-10-27
    相关资源
    最近更新 更多