jQuery点击事件在一次点击后停止工作 [英] jQuery click event stops working after one click

查看:443
本文介绍了jQuery点击事件在一次点击后停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个div(#replyLoop),链接ID lmor 在单击时从href元素 data-posts 中拉取内容。工作正常的第一次点击,但后,它什么也没有。我怀疑它与代码的结构有关,而不是所使用的事件 - 活和点击有相同的结果,因为删除返回false;并且仅使用链接的data-posts元素并去除href部分。 (return false;禁用链接的href启动)。

I have a div (#replyLoop) that the link id lmor pulls content into from the href element data-posts when it is clicked. Works fine for the first click, but then after it does nothing. I suspect it has something to do with the structure of the code and not the events used - both "live" and "click" have the same result, as does removing return false; and only using the data-posts element of the link and removing the href portion. (return false; to disable the link's href from firing).

我总是似乎错过了最明显的事情,当它涉及到js - 我想知道如果这是

I always seem to miss the most obvious things when it comes to js - and I'm wondering if that's the case here.

(tooltip-e确保在新内容加载时调用 tipsy 函数。)

(tooltip-e is ensuring that the tipsy function gets called whenever the new content is loaded in. (tooltips))

现场标记 -

$(function(){
$("#lmor").live("click",function() {
    $('#replyLoop').load($(this).attr('data-posts'), function(data) {
            $('.tooltip-e').tipsy({
            gravity: 'e',
            fade: true,
            html: true
            });
        });
        return false;
    });
});

点击标记 -

$(function(){
$("#lmor").click(function() {
    $('#replyLoop').load($(this).attr('data-posts'), function(data) {
            $('.tooltip-e').tipsy({
            gravity: 'e',
            fade: true,
            html: true
            });
        });
    return false;
    });
});

有什么想法我缺少什么?

Any ideas on what I'm missing?

编辑
我想出来了 - 使用.live的组合,并在适当的文件中包含加载更多,我已经使它工作。

Edit I figured it out - using a combination of .live and including the "load more" in the appropriate file, I have gotten it to work.

问题:它正在被加载的页面之外被调用 - 因此当它正在寻找一个下一个页面时,下一个页面没有被推送到链接,因为它不知道它已经加载。

The problem: it was being called outside of the page that was loading - so that when it was looking for a "next" page, that "next" page hadn't been pushed to the link yet because it didn't know it had been loaded.

我也标记了实际代码使用's而不是谢谢您的帮助!

I have also marked up the live code to use 's instead of "s. Thanks for your help!

推荐答案

如果我们不需要猜测会更容易回答,但这里是我的猜测:#lmor within html你加载到#replyLoop当它第一次点击?我想是的,问题是$(#lmor)。live设置在DOM元素#lmor,然后当你加载新的内容那么你删除该dom元素并创建一个新的DOM元素,方法是:

Would be easier to answer if we wouldn't need to guess, but here is my guess: is #lmor within the html that you load into #replyLoop when it's clicked for the 1st time? I guess it is, and the problem is that $("#lmor").live is set on the DOM element #lmor, then when you load the new content then you delete that dom element and create a new one. The way to do it is:

$(document).on("click", "#lmor", function...);

这样当你创建新的#lmor时(加载新内容时),它也会获得事件

this way when you create the new #lmor (when loading the new content) it also gets the event listener.

优化版本可以是:

$("#replyLoop").on("click", "#lmor", function...);

如果#lmor位于#replyLoop内

if #lmor is inside #replyLoop

这篇关于jQuery点击事件在一次点击后停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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