【问题标题】:Apply function to only one div class, not all仅将函数应用于一个 div 类,而不是全部
【发布时间】:2011-06-15 00:58:22
【问题描述】:

我目前正在网站上的位置页面上工作,但在显示/隐藏 jquery 效果方面遇到了问题。下面的 jQuery 激活点击并应用切换 div 的类以显示/隐藏它......非常简单。但是,当我添加更多具有相同类的 div 并单击激活器链接时,它会在所有 div 上运行函数。我知道这是一个简单的修复,但我无法找出正确的语法。任何人都可以快速查看此代码并了解如何修复它。我只包括了下面的 html div 之一。当我有不止一个导致问题时。见example here

谢谢, 泰勒

jQuery

$(function(){
    $(".sliding").hide();
    $(".show-hide").show();
    $(".hide-click").show();

    $('.show-hide').click(function(){   
        $(".sliding").slideToggle("slow");
        $(".hide-click").slideToggle("slow");
    });
});

HTML

<!-- Bowie, MD -->
<div class="location" style="margin-top: 0;">

<!-- City Name -->
<h3><a href="javascript:void(0);" class="show-hide">Bowie</a></h3>

    <h2>Address</h2>
    <p>16901 Melford Blvd, Suite 11<br/>
    Bowie, MD 20715</p>

    <h2>Contact</h2>
    <p><span><strong>Phone:</strong> 301.805.5395</span><br />
    <span><strong>Fax:</strong> 301.805.5396</span></p>

    <span class="hide-click"><a href="javascript:void(0);" class="show-hide">View additional info</a></span>

    <!-- Hidden Content -->
    <div class="sliding">

        <h2>Map</h2>

        <div class="map">   
            <iframe width="211" height="140" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=16901+Melford+Blvd,+Suite+11+Bowie,+MD+20715&amp;sll=37.0625,-95.677068&amp;sspn=61.023673,112.236328&amp;ie=UTF8&amp;hq=&amp;hnear=16901+Melford+Blvd,+Bowie,+Maryland+20715&amp;ll=38.959742,-76.714354&amp;spn=0.009344,0.018196&amp;z=14&amp;iwloc=A&amp;output=embed"></iframe>
        </div>

        <a href="http://maps.google.com/maps?f=d&source=s_d&saddr=16901+Melford+Blvd,+Suite+11+Bowie,+MD+20715&daddr=&hl=en&geocode=&mra=prev&sll=38.959741,-76.714349&sspn=0.00755,0.013422&g=16901+Melford+Blvd,+Suite+11+Bowie,+MD+20715&ie=UTF8&z=17" target="_blank" class="directions">Get Directions</a>

        <h2>Doctors</h2>
        <p><a href="/#phillips.php">William B. Phillips, II M.D.</a>, <a href="/#weichel.php">Eric D. Weichel, M.D.</a></p>
        <a href="javascript:void(0);" class="show-hide">Hide additional info</a>

    </div>
    <!-- end Hidden Content -->

</div>
<!-- end Location -->

【问题讨论】:

  • 感谢大家的帮助。我的脑海中肯定有正确的逻辑,我正在尝试 .parent() 和其他委托选择器,但@joekarl 真的通过查找父元素然后从那里返回列表以使其特定于当前分区。正是我需要的。再次感谢大家。

标签: jquery css html


【解决方案1】:

如果我理解正确,您只想切换与其 .show-hide 链接关联的 .sliding 内容。您可以设置 jQuery 上下文以找到正确的 .sliding 元素。这样您就不会假设元素位于某个位置,并且您不会被绑定到唯一的 css id,从而提供更大的灵活性。

$(function(){
    $(".sliding").hide();
    $(".show-hide").show();
    $(".hide-click").show();

    $('.show-hide').click(function(){   
        //find the surrounding div
        var location = $(this).parent().parent();

        //find the correct elements within the location block
        $(".sliding",location).slideToggle("slow");
        $(".hide-click",location).slideToggle("slow");
    });
});

【讨论】:

    【解决方案2】:

    您可以使用:first:nth-child 选择器。

    查看 JQuery 文档:

    http://api.jquery.com/nth-child-selector/

    http://api.jquery.com/first-selector/

    例子:

    $(".sliding").hide();
    

    【讨论】:

      【解决方案3】:

      你可以给你的div一个id,然后在你的jQuery选择器中使用#divID来只选择那个div

      $('#divID').hide();

      ID 的全部意义在于它只能应用于页面中的一个元素,而类可以应用于任意数量的元素。

      【讨论】:

        【解决方案4】:

        两个选项是:

        1) 使用 id 选择器作为 $()​​ 的参数,并将 id 属性添加到您想要的一个 div,

        &lt;div id="someid"&gt; 然后js看起来像这样$("#someid").show()

        2) 使用 $( $().get(N) ).show() 其中 N 是您想要影响的 div 的已知索引。但是,很难推荐此解决方案,因为如果您重新格式化页面,它更有可能崩溃。

        【讨论】:

          【解决方案5】:

          对于你可以使用的 div

            $(this).parents(".sliding").slideToggle("slow")
          

          【讨论】:

            【解决方案6】:

            我会这样做的。

            首先设置除第一个以外的所有 div,在您的 css 中将 dn 类设置为 display:none

            然后你的 javascript 你可以做这样的事情。 我假设您希望您的点击触发器始终可见。

            $(function(){
            
                $('.show-hide').click(function(){
            
                    $(".sliding").slideUp("slow");
                    $(this).next(".sliding).slideDown("slow");
            
               });
            });
            

            【讨论】:

              【解决方案7】:

              例如:

              $(".sliding:eq(0)").slideToggle("slow");  
              

              将其设置为“.sliding”类的第一个元素。

              见:How to get nth jQuery element

              【讨论】:

              • 大家好,谢谢您的回答。这不起作用,因为我不希望它只激活页面上的第一项。我希望在单击页面时将相同的脚本应用于页面上的其他 div。请在此处查看示例:rgw.steadfastdesignfirm.com/#locations.php
              猜你喜欢
              • 1970-01-01
              • 2020-11-07
              • 1970-01-01
              • 1970-01-01
              • 2016-05-24
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多