点击事件触发两次

发布于 2024-09-25 16:51:59 字数 881 浏览 0 评论 0原文

我不知道这是否与 Raphael、ColorBox 或 jQuery 有关。这是我的相关代码:

var image = paper.image(p.url_, tx, ty, img.width, img.height);
image[0].style.cursor = "pointer";
image.node.onclick = function() {
    $.colorbox({
        title: "Some Random Title",
        href: function() {
            $.post("test.php", { arg: p.id_ } );
        }
    });
};      

当观看 FireBug 控制台中的点击时,该帖子被触发两次。如果它也是 get 的话,它也会被触发两次。

如果我从 href 函数更改为直接调用 test.php,则只会发出一个 get 。

为什么使用 jQuery 时单击事件会发出两次调用?

更新:在匿名函数中添加对警报的调用也会触发两次,所以我猜这与 colorbox 有关。

更新2:刚刚尝试将其连接到实际页面,FireBug 除了两个 get/posts 之外还吐出此错误消息。这证实了这是 ColorBox 中的问题。此外,它也使进展变得困难,因为虽然两者都调用完整的彩盒,但它却显示了它的颤动。

c is undefined 
    (function(b,gb){var v="none",t="click"...c.settings=eb;b(c.init)})(jQuery,this)

注意:这仅附加到第二次调用,而不是第一次。

I don't know if this is to do with Raphael, ColorBox or jQuery. Here's my the relevant code:

var image = paper.image(p.url_, tx, ty, img.width, img.height);
image[0].style.cursor = "pointer";
image.node.onclick = function() {
    $.colorbox({
        title: "Some Random Title",
        href: function() {
            $.post("test.php", { arg: p.id_ } );
        }
    });
};      

When watching the click in FireBug console the post is fired twice. It is also fired twice if it is a get as well.

If I change from the href function to a direct call of test.php then there is only a single get issued.

Why is the click event issuing the call twice when using jQuery?

UPDATE: Adding a call to alert in the anonymous function also fires twice so I guess it is something to do with colorbox.

UPDATE 2: Just tried wiring it to an actual page and FireBug spits this error message out in addition to the two get/posts. Which confirms this is a problem in ColorBox. Also it makes progress hard because while both calls complete colorbox sits showing it's throbber.

c is undefined 
    (function(b,gb){var v="none",t="click"...c.settings=eb;b(c.init)})(jQuery,this)

Note: This only attached to the second call and not the first.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

五里雾 2024-10-02 16:51:59

对 colorbox 不太熟悉,所以我在这里冒险。我猜你希望 colorbox 的 html 使用帖子的结果。我会把帖子放在彩盒外面。您有什么原因不使用 $(image.node).click() 吗?甚至$(image).click()?这是我的做法

var image = paper.image(p.url_, tx, ty, img.width, img.height);
image[0].style.cursor = "pointer";
$(image.node).click(function() {
    var post_html = $.post("test.php", { arg: p.id_ } );
    $.colorbox({
        title: "Some Random Title",
        html: post_html
    });
});

这可能无法解决问题,但它会告诉您问题是否是 colorbox 触发了 href 两次,或者单击是否被执行了两次。

Not terribly familiar with colorbox so I am going out on a limb here. I'm guessing you want colorbox's html to use the result of the post. I would put the post outside of color box. Is there any reason you aren't using $(image.node).click()? Or even $(image).click()? Here's how I would do it

var image = paper.image(p.url_, tx, ty, img.width, img.height);
image[0].style.cursor = "pointer";
$(image.node).click(function() {
    var post_html = $.post("test.php", { arg: p.id_ } );
    $.colorbox({
        title: "Some Random Title",
        html: post_html
    });
});

This may not fix it, but it will tell you if the problem is that colorbox fires the href twice or if the click is being executed twice.

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