Jquery 尝试在单击子元素时隐藏元素

发布于 2024-08-10 01:32:21 字数 688 浏览 2 评论 0原文

我试图在单击 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 技术交流群。

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

发布评论

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

评论(2

土豪我们做朋友吧 2024-08-17 01:32:21

设置点击功能时,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

对不⑦ 2024-08-17 01:32:21

当您绑定到点击事件时,DOM 中是否存在 #close ?如果您使用 ajax 或其他方式修改 DOM,则需要在代码中的该位置设置点击事件或使用 live

$("#close").live("click", function() { do stuff });

此外,您应该从点击事件返回 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 use live.

$("#close").live("click", function() { do stuff });

Also you should be returning false from your click event or calling preventDefault.

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