更改ID后JQuery功能不起作用
我有一个简单的文件,其中包含一个 Jquery 脚本,如下所示:
<script type="text\javascript">
$('.grey_title').click(function(){
$(this).parent().find('.inner_div').slideToggle('slow');
});
$('#hideall').click(function(){
$('.inner_div').slideUp('slow');
$(this).parent().html("<span id=\"showall\">Show all Menus</span>");
});
$('#showall').click(function(){
$('.inner_div').slideDown('slow');
$(this).parent().html("<span id=\"hideall\">Hide all Menus</span>");
});
});
</script>
<div><span id="hideall">Hide all Menus</span></div>
该函数在隐藏菜单时工作正常,并且当您将 ID 更改为 HTML 中的 showall 并将脚本更改为 slipToggle 时,但是当您单击“隐藏全部”时,它将关闭根据 Firefox 的说法,将所有项目更改为
<span id="showall">...</span>
,但是,当再次单击时,它什么也不做。我可能做错了什么?
页面
I have a simple file that has a Jquery script it in that looks like this:
<script type="text\javascript">
$('.grey_title').click(function(){
$(this).parent().find('.inner_div').slideToggle('slow');
});
$('#hideall').click(function(){
$('.inner_div').slideUp('slow');
$(this).parent().html("<span id=\"showall\">Show all Menus</span>");
});
$('#showall').click(function(){
$('.inner_div').slideDown('slow');
$(this).parent().html("<span id=\"hideall\">Hide all Menus</span>");
});
});
</script>
<div><span id="hideall">Hide all Menus</span></div>
The function works fine while hiding menus and when you change the ID to showall in the HTML and the script to slideToggle, however when you click Hide all it will close all and according to Firefox, changes the item to be
<span id="showall">...</span>
but, when clicked again it does nothing. What could I be doing wrong?
the page
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我预计绑定会失败,因为绑定完成后没有显示全部
更好的选择是 toggle
您可能需要切换功能以匹配页面加载时显示的内容
此处演示
I expect the binding to fail since there is no showall when the binding is done
A better choice is to toggle
You may want to switch the functions around to match what is shown when the page loads
DEMO HERE
当 id 消失时,它会分离与其绑定的事件。您可以尝试
live
事件绑定。When the id is gone, it detaches the event bound to it. You can try the
live
event binding.您需要使用 delegate 或 live 来定义事件。上面的事件所做的只是将处理程序绑定到页面上的现有元素,而 live/delegate 函数则绑定到现有和未来的元素。
You need to use delegate or live to define the events. What the events above do is only bind the handler to the existing elements on the page while the live/delegate function bind to existing and future elements.
您正在将单击事件绑定到尚不存在的元素...尝试以下操作:
You are binding to the click event to an element that does not yet exist... try this: