无论用户在文档中的何处单击,Livequery都会触发单击 [英] Livequery fires click no matter where the user clicks in the document

查看:128
本文介绍了无论用户在文档中的何处单击,Livequery都会触发单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击触发图像时,我用一个漂亮的小弹出窗口替换了传统的选择/选项表单元素.该页面用于会计目的,因此需要多个订单项.我已经编写了JavaScript,它将动态生成新的订单项选择/选项元素.页面加载时,将加载初始选项集,用户可以单击它们,弹出带有某些选项的弹出窗口,选择一个选项,然后关闭该框.移至下一个选择,依此类推.我已将livequery添加到这些动态元素的代码中.但是,无论用户在页面上何处单击,livequery("click" ...)似乎都会触发.非常令人沮丧.

I have replaced the traditional select/option form elements with a nifty little popup window when a triggering image is clicked. The page is for accounting purposes and so multiple line items are to be expected. I've written the javascript that will dynamically generate new line item select/option elements. When the page loads, the initial set of choices loads and the user can click on them, get a pop up with some choices, choose one and then the box closes. The move to the next choice and so on and so forth. I've added livequery to my code for those dynamic elements. However... the livequery("click"...) seems to fire no matter where the user clicks on the page. Very frustrating.

我在这里已经阅读了jQuery 1.3中"live()"的功能,但是我无法完全升级到jquery 1.3,因为自定义JS文件依赖于1.2,因此使用live()超出了问题,但是我调用了livequery()插件,我真的需要了解我是否正确使用了它.

I've read on here how great "live()" is in jQuery 1.3, but I am not able to upgrade fully to jquery 1.3 because a custom JS file depends on 1.2, so using live() is out of the question, however I have invoked the livequery() plugin and I really need to understand if I'm using it correctly.

我将发布部分代码.全部发布的方式太多了.

I will post partial code. There's just way too much to post all of it.

基本上,我正在搜索以"bubble"开头的div,然后是一个数字.然后在每个事件上运行事件.只有bubble1是静态的,2和up是动态的.我是否错过了livequery的全部用法?

Basically, I'm searching for divs starting with "bubble" and then a number afterwards. Then run the event on each them. Only bubble1 is static, 2 and up are dynamic. Am I missing the whole usage of livequery?

>$jb('div[id^="bubble"]').each(function () {
> var divid = $jb('div[id^="bubble"]').filter(":first").attr("id");
>var pref = "bubble";
>var i = divid.substring((pref.length));
>var trigger = $jb('#trigger' + i, this);
>var popup = $jb('#pop'+ i, this).css('opacity', 0);
>var selectedoption = $jb('selectedOption' + i, this);
>var selectedtext = $jb('selectedOptionText' + i, this);
>$jb([trigger.get(0), popup.get(0)]).livequery("click",
> function () {
>//alert(i);
// code removed for brevity (just the contents of the popups)
>});

推荐答案

Live通过使用事件委托进行工作.单击事件附加到主体,并且每当单击某项时,都会针对目标测试选择器.如果通过选择器测试,它将调用该函数(从而模拟click事件).

Live works by using event delegation. A click event is attached to the body, and anytime something is clicked the selector is tested against the target. If it passes the selector test it calls the function (thus simulating a click event).

您可能想要这样的东西:

You probably want something like this:

$('div[id^="bubble"]').livequery("click", function() {
    var divId = $(this).attr("id");
    var i = divId.substring("bubble".length);
    var trigger = $("#trigger" + i, this);
    var popup = $("#pop" + i, this).css("opacity", 0);
    // alert(i);
}

这篇关于无论用户在文档中的何处单击,Livequery都会触发单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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