jQuery live() ...必须单击两次才能激活链接?
我有以下代码,简单地说:
$(function() {
$('a.add-photos-link').live('click', function(e) {
$(this).colorbox({
overlayClose: false,
onComplete: function() {
$('#add_photos').submit(function(e) {
// more stuff to do
e.preventDefault();
});
}
});
e.preventDefault();
});
});
但是,这似乎只有在单击链接两次后才起作用。这些链接会动态添加到页面 (a.add-photos-link
)。
为什么会发生这种情况?我可以采取什么措施来修复它,以便它在第一次单击后触发?
I have the following bit of code, simply:
$(function() {
$('a.add-photos-link').live('click', function(e) {
$(this).colorbox({
overlayClose: false,
onComplete: function() {
$('#add_photos').submit(function(e) {
// more stuff to do
e.preventDefault();
});
}
});
e.preventDefault();
});
});
However, this only seems to work after single-clicking on the link TWICE. These links are dynamically added to the page (a.add-photos-link
).
Why is this happening and what can I do to fix it so it fires after the first single-click?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您当前的代码仅为链接创建一个颜色框。它不会打开颜色框,这就是为什么您需要单击链接两次:一次创建它,再次打开它。
创建颜色盒时,您可以使用
open
选项(如文档所述)将其打开立即,像这样:Your current code only creates a colorbox for the link. It does not open the colorbox, which is why you need to click the link twice: once to create it and again to open it.
You can use the
open
option (as documented) when creating the colorbox to open it immediately, like so: