Jquery 尝试在单击子元素时隐藏元素
我试图在单击 ID 为“close”的图像时隐藏元素。
$('#close').click(function() {
$('#ordercell').hide('slide');
});
据我所知,应该是我所需要的,但当我点击时什么也没有发生。
$(document).keyup(function(event) {
if (event.keyCode ==27) {
$('#ordercell').hide('slide');
}
});
当按下转义键时,隐藏效果很好,所以我不太确定我错过了什么。
我的 HTML 是(不使用图像 ATM,因为我想在制作脚本之前我会让脚本工作):
<div id="ordercell">
<div id="orderform">
<div class="cardorder" id="cardorder56">
<div id="close">X</div>
<img src="foo.jpg">
</div>
</div>
</div>
这都是硬编码的,没有 AJAX。
I am trying to hide an element when an image with the ID of 'close' is clicked.
$('#close').click(function() {
$('#ordercell').hide('slide');
});
Should be all I need, from what I can tell, but nothing is happening when I click.
$(document).keyup(function(event) {
if (event.keyCode ==27) {
$('#ordercell').hide('slide');
}
});
Is working just fine to hide when escape is pressed, so I'm not quite sure what I'm missing.
My HTML is (not using an image ATM because i figured i'd get the script working before i made one):
<div id="ordercell">
<div id="orderform">
<div class="cardorder" id="cardorder56">
<div id="close">X</div>
<img src="foo.jpg">
</div>
</div>
</div>
This is all hard coded, no AJAX.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
设置点击功能时,DOM中是否已经存在
id="close"
的图片?顺便提一句。世界FM。检查这里 http://jsbin.com/acose
Does the image with
id="close"
already exist in the DOM when you set the click function?Btw. WFM. Check here http://jsbin.com/acose
当您绑定到点击事件时,DOM 中是否存在
#close
?如果您使用 ajax 或其他方式修改 DOM,则需要在代码中的该位置设置点击事件或使用live
。此外,您应该从点击事件返回 false 或调用 PreventDefault。
Does
#close
exist in the DOM when you're binding to the click event? If you're using ajax or some other means to modify the DOM you'll need to setup your click event at that point in your code or uselive
.Also you should be returning false from your click event or calling preventDefault.