jQuery 隐藏/显示(带有修改的 id 属性的 div)

发布于 2024-12-07 14:36:25 字数 1300 浏览 1 评论 0原文

我在执行此操作时遇到了麻烦:

<div id="show-menu">Show Menu</div>

    <script type="text/javascript"> 
        $(document).ready(function() {
            $('#show-menu').click(function() {
                $(this).animate({marginRight:'70px'}, 500);
                $('#menu').animate({width:'300px'}, 500);
                $('.menu-menu-principal-container, #header h1').animate({marginRight:'70px'}, 500);
                $(".menu-menu-principal-container, #header h1").show("slow");
                $("#show-menu").hide("slow");
                $("#hide-menu").show("slow");
                $(this).text('Hide Menu');
                $(this).attr('id', 'hide-menu');            
            });
            $('#hide-menu').click(function() {
                $(this).animate({marginRight:'-70px'}, 500);
                $('#menu').animate({width:'100px'}, 500);
                $('.menu-menu-principal-container, #header h1').animate({marginRight:'-70px'}, 500);
                $(".menu-menu-principal-container, #header h1").hide("slow");
                $(this).text('Show Menu');
                $(this).attr('id', 'show-menu');
            });
        })

    </script>

如果我单击“显示菜单”(#show-menu),它会正确显示,但是当我再次单击“隐藏菜单”(#hide-menu) 时,它不会隐藏?它什么也不做。

I'm having truoble doing this:

<div id="show-menu">Show Menu</div>

    <script type="text/javascript"> 
        $(document).ready(function() {
            $('#show-menu').click(function() {
                $(this).animate({marginRight:'70px'}, 500);
                $('#menu').animate({width:'300px'}, 500);
                $('.menu-menu-principal-container, #header h1').animate({marginRight:'70px'}, 500);
                $(".menu-menu-principal-container, #header h1").show("slow");
                $("#show-menu").hide("slow");
                $("#hide-menu").show("slow");
                $(this).text('Hide Menu');
                $(this).attr('id', 'hide-menu');            
            });
            $('#hide-menu').click(function() {
                $(this).animate({marginRight:'-70px'}, 500);
                $('#menu').animate({width:'100px'}, 500);
                $('.menu-menu-principal-container, #header h1').animate({marginRight:'-70px'}, 500);
                $(".menu-menu-principal-container, #header h1").hide("slow");
                $(this).text('Show Menu');
                $(this).attr('id', 'show-menu');
            });
        })

    </script>

If I click on Show Menu (#show-menu) it shows correctly, but when I click again in the Hide Menu (#hide-menu) it wont hide? It does nothing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

荒人说梦 2024-12-14 14:36:25

您需要使用 jQuery delegate()live()

我最好使用这样的委托

$('body').delegate('#show-menu', 'click', function() { ... your code ... });
$('body').delegate('#hide-menu', 'click', function() { ... your code ... });

请记住,您可以从 DOM 中的不同位置进行委托,而不是 $('body').delegate( ); 你可以使用$('#myparentContainer').delegate();

另一种方法是使用像这样的实时事件

$('#show-menu').live('click', function() { ... your code ...});
$('#hide-menu').live('click', function() { ... your code ...});

You need to use jQuery delegate() or live()

Preferably I would use delegate like this

$('body').delegate('#show-menu', 'click', function() { ... your code ... });
$('body').delegate('#hide-menu', 'click', function() { ... your code ... });

Remember you can delegate from a different position in the DOM instead of $('body').delegate(); you could use $('#myparentContainer').delegate();

The alternative would be to use live events like this

$('#show-menu').live('click', function() { ... your code ...});
$('#hide-menu').live('click', function() { ... your code ...});
不必了 2024-12-14 14:36:25

隐藏菜单在文档就绪函数调用期间不存在,因此不会受到限制。
您需要使用 live 来限制事件的出现。

hide-menu does not exist during the document ready function call, and hence will not be bounded.
you would need to use live to have the event bounded as it appears.

深爱不及久伴 2024-12-14 14:36:25

.click() 的作用是将单击事件的函数绑定到选择器。由于您在 #hide-menu html 元素存在之前执行 $('#hide-menu').click(function() ,因此它不起作用,您应该放置 $('#hide-menu').click(function() )。 click(function() { after $(this).attr('id', 'hide-menu');

$(document).ready(function() {
            $('#show-menu').click(function() {
                $(this).animate({marginRight:'70px'}, 500);
                $('#menu').animate({width:'300px'}, 500);
                $('.menu-menu-principal-container, #header h1').animate({marginRight:'70px'}, 500);
                $(".menu-menu-principal-container, #header h1").show("slow");
                $("#show-menu").hide("slow");
                $("#hide-menu").show("slow");
                $(this).text('Hide Menu');
                $(this).attr('id', 'hide-menu');  
$('#hide-menu').click(function() {
                $(this).animate({marginRight:'-70px'}, 500);
                $('#menu').animate({width:'100px'}, 500);
                $('.menu-menu-principal-container, #header h1').animate({marginRight:'-70px'}, 500);
                $(".menu-menu-principal-container, #header h1").hide("slow");
                $(this).text('Show Menu');
                $(this).attr('id', 'show-menu');
            });          
            });

        })

您也可以使用 live() 因为它“现在或将来绑定”,但这效率较低,因为您“知道”当 #hide-menu 属性出现在 DOM 中时,您应该在那里绑定事件

What .click() does is to bind a function to the click event to the selector. Since you're doing $('#hide-menu').click(function() before a #hide-menu html element exist, it won't work, you should place the $('#hide-menu').click(function() { after $(this).attr('id', 'hide-menu');

$(document).ready(function() {
            $('#show-menu').click(function() {
                $(this).animate({marginRight:'70px'}, 500);
                $('#menu').animate({width:'300px'}, 500);
                $('.menu-menu-principal-container, #header h1').animate({marginRight:'70px'}, 500);
                $(".menu-menu-principal-container, #header h1").show("slow");
                $("#show-menu").hide("slow");
                $("#hide-menu").show("slow");
                $(this).text('Hide Menu');
                $(this).attr('id', 'hide-menu');  
$('#hide-menu').click(function() {
                $(this).animate({marginRight:'-70px'}, 500);
                $('#menu').animate({width:'100px'}, 500);
                $('.menu-menu-principal-container, #header h1').animate({marginRight:'-70px'}, 500);
                $(".menu-menu-principal-container, #header h1").hide("slow");
                $(this).text('Show Menu');
                $(this).attr('id', 'show-menu');
            });          
            });

        })

you can also use live() since it "binds now or in the future" but that's less efficient, since you "know" when the #hide-menu attribute appear in the DOM, you should bind the event there

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文