【问题标题】:Stop jQuery sliding up when clicked to next page单击下一页时停止jQuery向上滑动
【发布时间】:2011-09-15 21:36:49
【问题描述】:

我不知道这是否可能。我有一个 jquery 下拉菜单,当我单击幻灯片菜单中的一个链接时,我不希望滑块关闭。当我到达点击链接的目标页面时,我希望它保持打开状态。

这是我的代码:index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Advice center</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-5">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){
        $("#flip_1").click(function(){
            $("#panel_1").slideToggle("slow");
        });
    });
</script>

<style type="text/css">
.nodisplay{
 display:none; 
}
</style>
</head>
<body>
<ul>
    <li><a href="page1.html">Page 1</a></li>
    <li><a href="page2.html">Page 2</a></li>
    <li><a href="page3.html">Page 3</a></li>
    <li><a href="page4.html">Page 4</a></li>

    <li id="flip_1"> <span>Slide here</span></li>
        <div id="panel_1" class="nodisplay">
            <ul>
                <li><a href="section1.html">section 1</a></li>
                <li><a href="section2.html">section 2</a></li>

              </ul> 
        </div>  

       <li><a href="">bottom 1</a></li>
       <li><a href="">bottom 2</a></li>
     </ul>
</body>
</html>

这是链接到 section1.html

的页面之一
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Advice center</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-5">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){
        $("#flip_1").click(function(){
            $("#panel_1").slideToggle("slow");
        });     
       });
</script>

<style type="text/css">
.nodisplay{
 display:none; 
}
</style>

</head>

<body>

<ul>
    <li><a href="page1.html">Page 1</a></li>
    <li><a href="page2.html">Page 2</a></li>
    <li><a href="page3.html">Page 3</a></li>
    <li><a href="page4.html">Page 4</a></li>

    <li id="flip_1"> <span>Slide here</span></li>
        <div id="panel_1" class="nodisplay">
            <ul>
                <li><a href="section1.html">section 1</a></li>
                <li><a href="section2.html">section 2</a></li>

              </ul> 
        </div>  

       <li><a href="">bottom 1</a></li>
       <li><a href="">bottom 2</a></li>
     </ul>

Welcome to section 1
</body>
</html>

第三页如下:section2.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Advice center</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-5">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){
        $("#flip_1").click(function(){
            $("#panel_1").slideToggle("slow");
        });

    });
</script>

<style type="text/css">
.nodisplay{
 display:none; 
}
</style>

</head>

<body>

<ul>
    <li><a href="page1.html">Page 1</a></li>
    <li><a href="page2.html">Page 2</a></li>
    <li><a href="page3.html">Page 3</a></li>
    <li><a href="page4.html">Page 4</a></li>

    <li id="flip_1"> <span>Slide here</span></li>
        <div id="panel_1" class="nodisplay">
            <ul>
                <li><a href="section1.html">section 1</a></li>
                <li><a href="section2.html">section 2</a></li>

              </ul> 
        </div>  

       <li><a href="">bottom 1</a></li>
       <li><a href="">bottom 2</a></li>
     </ul>

This is section 2 (TWO) page
</body>
</html>

【问题讨论】:

  • 您不能在子页面上将.slideToggle("slow") 替换为show() 吗?
  • @mblase75 - 谢谢你,但它不起作用。
  • 尝试使用slideDown和slideUp。

标签: javascript jquery html jquery-slider


【解决方案1】:

如果您的某个链接被点击,请尝试以下操作(您的 .click 事件绑定中需要 if/else):

$("#panel_1").stop();

【讨论】:

    【解决方案2】:

    在内部链接上,使用哈希来识别它们

    <a href="section1.html#panel">Section 1</a>
    

    然后onPageLoad你可以检查是否有哈希并使用它打开:

    if(window.location.hash == "#panel") $("#panel_1").slideDown();
    

    另外,对您的代码进行一个小修复:将 div 放在 li 中

    <li id="flip_1"><span>Slide here</span>
        <div id="panel_1" class="nodisplay">
            <ul>
                <li><a href="section1.html">section 1</a></li>
                <li><a href="section2.html">section 2</a></li>
            </ul> 
        </div> 
    </li>
    

    然后用这个来切换:

    $("#flip_1 span").click(function(){
            $("#panel_1").slideToggle("slow");
    });
    

    【讨论】:

      猜你喜欢
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      • 2011-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多