jQuery 隐藏/显示(带有修改的 id 属性的 div)
我在执行此操作时遇到了麻烦:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使用 jQuery delegate() 或 live()
我最好使用这样的委托
请记住,您可以从 DOM 中的不同位置进行委托,而不是
$('body').delegate( );
你可以使用$('#myparentContainer').delegate();
另一种方法是使用像这样的实时事件
You need to use jQuery delegate() or live()
Preferably I would use delegate like this
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
隐藏菜单在文档就绪函数调用期间不存在,因此不会受到限制。
您需要使用 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.
.click() 的作用是将单击事件的函数绑定到选择器。由于您在 #hide-menu html 元素存在之前执行 $('#hide-menu').click(function() ,因此它不起作用,您应该放置 $('#hide-menu').click(function() )。 click(function() { after $(this).attr('id', 'hide-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');
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