【问题标题】:hide and show div click on a隐藏和显示 div 点击一个
【发布时间】:2013-10-30 20:23:53
【问题描述】:

我想隐藏 id 当我点击一个 href 时,它的相同 ID 显示和其他 ID 将自动关闭

示例:

<div id="fit" class="heading">FIT</div>
<a href="#er">first</a>
<div id="er" style="display:none;">aksdjfhaskdj hskj hskjfh sd fghjgfdjf gdsjfdg jdfgjdf gjgdfjgfdjf gasdkjfasjfghsdj </div>
<a href="#erp">erp</a>
<div id="erp" style="display:none;">erp </div>
<div id="style" class="heading">style</div>

和脚本:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function(e) {
    $("a").click(function(e) {
        var ab = $(this).attr("href");
        //alert(ab);
        //$("div").hide();
        $(ab).show();

    });
});
</script>

【问题讨论】:

    标签: javascript php jquery html css


    【解决方案1】:

    在 html 中使用类作为锚相关的 div

    <div id="fit" class="heading">FIT</div>
    <a href="#er">first</a>
    <div id="er" style="display:none;" class="anchorrel">aksdjfhaskdj hskj hskjfh sd fghjgfdjf gdsjfdg jdfgjdf gjgdfjgfdjf gasdkjfasjfghsdj </div>
    <a href="#erp">erp</a>
    <div id="erp" style="display:none;" class="anchorrel">erp </div>
    <div id="style" class="heading">style</div>
    
    
    
    
    <script>
        $(document).ready(function(e) {
            $("a").click(function(e) {
            e.preventDefault();
                var ab = $(this).attr("href");
                //alert(ab);
                $(".anchorrel").hide();// all div related to .anchorrel hide
                $(ab).show();
    
            });
        });
        </script>
    

    demo

    【讨论】:

      【解决方案2】:

      如果您不想关闭所有其他 div 并仅显示 id 与单击的标签的 href 匹配的 div,请使用以下内容:

      $("a").click(function(e) {
          var ab = $(this).attr("href");
          $('div').hide();
          $(ab).show();
      });
      

      我不知道你为什么首先评论它,也许我不明白你想要实现什么。

      这是一个小提琴:http://jsfiddle.net/25RZR/

      【讨论】:

      • 它会隐藏上下文中的所有div,似乎不是OP的意图
      • "和其他 id 会自动关闭" - 这不是很容易理解。 :)
      【解决方案3】:

      你可以这样做:

      $(document).ready(function (e) {
      
          // Cache the anchor tag here
          var $link = $('a');
      
          // Click event handler for the link
          $link.click(function (e) {
      
              // prevent the default action of the anchor
              e.preventDefault();
      
              var id = this.href;
      
              // Hide all the visible divs next to all the anchors
              $link.next('div:visible').hide();
      
              // Show the current div with id
              $(id).show();
          });
      });
      

      【讨论】:

        【解决方案4】:

        Fiddle

        JavaScript

        $(document).ready(function(e) {
            $("a").click(function(e) {
                var ab = $(this).attr("href");  
                $('.content').hide();
                $(ab).show();
        
            });
        });
        

        HTML

        <div id="fit" class="heading">FIT</div>
        <a href="#er">first</a>
        <div id="er" class="content hide">aksdjfhaskdj hskj hskjfh sd fghjgfdjf gdsjfdg jdfgjdf gjgdfjgfdjf gasdkjfasjfghsdj </div>
        <a href="#erp">erp</a>
        <div id="erp" class="content hide">erp </div>
        <div id="style" class="heading">style</div>
        

        【讨论】:

          【解决方案5】:

          这样你可以做得更好:

          $(document).ready(function(e) {
            $("a").click(function (e) {
                e.preventDefault; // <------------stop the default behavior
                var ab = $(this).attr('href'); // <------better to use in terms of performance
                $(ab).show().siblings('div:not(.heading)').hide();
            });
          });
          

          Demo in action

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-02-12
            • 1970-01-01
            • 2017-04-19
            • 2017-07-22
            • 2023-03-10
            • 1970-01-01
            • 1970-01-01
            • 2011-01-15
            相关资源
            最近更新 更多