【问题标题】:Show Hide DIV with Jquery使用 Jquery 显示隐藏 DIV
【发布时间】:2011-08-20 21:12:53
【问题描述】:

我有这个简单的 JQuery Show/Hide 函数,它显然可以显示和隐藏一个 div。 有三件事我一个人是做不到的。

<script type="text/javascript">
  $(document).ready(function() {
  $('#showHideBox').show();

  $('#showHidetoggle').click(function() {
  $("#showHidetoggle").text("Show me")

    $('#showHideBox').slideToggle(250);
    return false;
  });
});
</script>

<a href="#" id="showHidetoggle">Hide Me</a>
  1. 我正在寻找一种将锚标记上的文本更改为显示/隐藏的方法。 我知道这已经被问过很多次了。但我似乎无法找到我的脚本的具体示例,到目前为止,文本在点击时会发生变化,但不会在后续点击时发生变化。

  2. 脚本通过将 div 滑出视图来隐藏它,但是,我需要 div 上的一部分可见,这样我可以附加用户将单击的按钮(锚点)标签。

    李>
  3. 最后,当用户单击隐藏时,div 会滑出视图,仅当用户刷新页面时才会重新出现。如何使 div 保持隐藏状态,但仅在用户单击按钮时出现?

可以在constantcontact.com 上找到此页面上我正在尝试完成的示例。查看页脚,您会看到它滑出视图,但按钮仍然可见。

任何帮助将不胜感激。

谢谢大家。

【问题讨论】:

    标签: jquery html event-handling visibility


    【解决方案1】:

    如何使用 Jquery 和 java 脚本隐藏和显示标签

    $(文档).ready(函数 () { $("#purchase_order").click(function () { $(".salep").hide(); $(".saleo").show(); $("#purchase_order").hide(); $("#sale_order").show(); });

    【讨论】:

    • '#' 表示 id '.'指示类
    • 这样你就可以调整类中的 div 和 id 中的按钮
    【解决方案2】:
    1. 我已将 following 幻灯片效果教程中的代码更改为替换 在 JQuery 点击事件上隐藏和显示锚标记。

      可以在JSFiddle 中看到以下代码的工作示例:

      $(document).ready(function () {
      
          $("#hide").click(function () {
              $(".target").hide("slide", {
                  direction: "up"
              }, 500);
              $('#show').show();
              $('#hide').hide();
          });
      
          $("#show").click(function () {
              $(".target").show("slide", {
               direction: "up"
              }, 500);
              $('#show').hide();
              $('#hide').show();
          });
      
      
          if ($('.target').is(':visible')) {
      
          }
      
      });
      

    【讨论】:

      【解决方案3】:

      因此,要做到这一点,最简单的方法 (imo) 是为您的按钮 + 框(您想要隐藏的)创建一个容器。您的按钮将始终保持不变。当页面加载时,我们会在您的按钮上附加一个事件,该事件将根据框的当前状态显示和隐藏您的框(如果它是隐藏的,则显示它,如果它是可见的,则隐藏它)。

      很简单。

      现在,您还希望在页面加载/页面访问之间保持可见/隐藏状态。要做到这一点,您需要在用户的浏览器上安装一个 cookie(旁注,如果您的用户已登录或其他方式,您可以通过其他方式执行此操作 - 但 cookie 是完成它的最简单的方法,并且没有t 涉及服务器往返以将数据保存到数据库)。

      为了做到这一点,我建议去获取jQuery Cookie Plugin,它使用起来非常简单(如下所示),并且在处理 cookie 时可以省去很多麻烦。每次您的用户单击您的按钮并更改框的状态时,您都将向用户的浏览器写入一个 cookie 并存储框的当前状态。这样,当用户重新加载页面时,您将知道当前状态是什么(因为 cookie)。在下面的示例中,我将 cookie 设置为一年后过期,但您可以根据需要更改。

      <div id="ShowHideContainer">
          <p><a id="ShowHideButton">Click Here To Hide!</a></p>
          <div id="ShowHideBox">text that gets shown and hidden, woo</div>
      </div>
      
      <script>
      $(document).ready(function()
      {
          var $ShowHideBox = $('#ShowHideBox').hide(),
              $ShowHideButton = $('#ShowHideButton');
      
          /* Initialize the box based on the state of the cookie in the user's browser*/
          initBox();
      
          $('#ShowHideButton').click(function() {
      
              if (boxVisible())
              {
                  //the box is visible. lets hide it.
                  hideBox();
              }
              else
              {
                  //the box is invisible. show it.
                  showBox();
              }
          });
      
          function initBox()
          {
              //if the cookie says this is visible, and it's not...show it
              if ( $.cookie('BoxVisible') && ! boxVisible() )
              {
                  showBox();
              }
              //if the cookie says this is not visible, and it is...hide it
              else if ( ! $.cookie('BoxVisible') && boxVisible() )
              {
                  hideBox();
              }
          }  
      
          //check to see if the box is visible or not, currently
          function boxVisible()
          {
              return $ShowHideBox.hasClass('hidden')? false : true;
          }
      
          //show the box, change the button text, and set the cookie
          function showBox()
          {
              $ShowHideBox.slideUp(250).removeClass('hidden');
              $ShowHideButton.html("Click Here to Hide!");
              $.cookie('BoxVisible', 1, {expires: 365});
          }
      
          //hide the box, change the button text, and set the cookie
          function hideBox()
          {
              $ShowHideBox.slideDown(250).addClass('hidden');
              $ShowHideButton.html("Click Here to Show!");
              $.cookie('BoxVisible', 0, {expires: 365});
          }
      });
      </script>
      

      【讨论】:

        【解决方案4】:
        $('#showHidetoggle').click(function() {  
            var boxText = $('#showHideBox').is(":visible") ? "Show me" : "Hide me";
            $("#showHidetoggle").text(boxText);
            $('#showHideBox').slideToggle(250);
            return false;
        });
        

        要保存状态,您需要使用服务器端代码或使用 cookie。

        【讨论】:

          【解决方案5】:
          1. 您可以使用if ($('#showHideBox').is(':visible') == True) {/*change text*/} else {/*change text*/} 将文本更改为根据需要显示/隐藏。
          2. 您需要将显示/隐藏按钮放在要隐藏的框之外。
          3. 您可以使用 cookie 或会话或 Local Storage 来存储需要在页面加载时保持不变的信息。您需要在页面加载时将框的初始状态设置为显示/隐藏。

          【讨论】:

            【解决方案6】:
            $(document).ready(function() {
                $('#showHideBox').show();
            
                $('#showHidetoggle:not(.hideme)').click(function() {
                    $(this).html("Hide me").addClass("hideme");
                    $('#showHideBox').animate({height: '100px'}, 250); // Or whatever height you want
                    return false;
                });
            
                $('#showHidetoggle.hideme').click(function() {
                    $(this).html("Show me").removeClass("hideme");
                    $('#showHideBox').animate({height: '300px'}, 250); // Or whatever height it was orginally.
                    return false;
                });
            
            });
            

            应该做的伎俩。

            【讨论】:

            • 这不是比需要的复杂吗?
            • 如果他想动画到某个高度以保持按钮可见,使用 is visible 将不起作用 - 但是,这可能不是最好的方法!
            猜你喜欢
            • 1970-01-01
            • 2011-11-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-01-04
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多