jQuery-如何将事件绑定到稍后将显示的隐藏元素? [英] jQuery - How do I bind events to hidden elements that will be shown later?

查看:138
本文介绍了jQuery-如何将事件绑定到稍后将显示的隐藏元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将'click'事件附加到特定类的所有元素.问题是绑定事件时,某些元素位于选项卡上处于隐藏状态(显示:无). (.bind()).似乎当显示这些元素时,事件不再受约束.

I am trying to attach 'click' events to all elements of a particular class. The problem is some of the elements are on a tab that is hidden (display: none) at the time that the event is bound. (.bind()). It seems that when these elements are shown the events are no longer bound.

$('a.someClass').bind('click', function(){
  alert("test");
});

隐藏的元素似乎没有绑定单击事件.如果我选择隐藏元素:

The hidden elements do not appear to have a click event bound. If I select the hidden elements:

$('a.someClass:hidden').bind('click', function(){
  alert("test");
});

当这些元素不再隐藏时,似乎没有绑定click事件.有人经历过吗?无论元素的显示属性如何,都可以将其绑定和事件化吗?

It seems the click event is not bound when these elements are no longer hidden. Has anyone experienced this? Is there a way to bind and event to elements irregardless of their display property?

谢谢

推荐答案

两年后编辑:正如某些人指出的那样,现在不推荐使用Live函数(如您在以下网址所看到的链接的文档页面顶部).当前版本的正确事件处理程序名称为On.请参见Maxim的答案以获取一个很好的例子.

edit 2 years later: As some people pointed out, the Live function is now deprecated (as you can also see at the top of the linked docs page). The right event handler name for the current version would be On. See Maxim's answer for a good example.

原始答案:
您是否尝试过使用 Live()?

Original answer:
Have you tried using Live()?

.live('click',function(){/* code */});

?

版本说明:live在jQuery 1.7中已弃用,在jQuery 1.9中已删除.此答案仅对早于jQuery 1.7的jQuery版本正确.

version note: live has been deprecated in jQuery 1.7 and it was removed in jQuery 1.9. This answer is only correct for jQuery versions older than jQuery 1.7

这篇关于jQuery-如何将事件绑定到稍后将显示的隐藏元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆