简体   繁体   English

触发jQuery中的自定义事件以与live()一起使用

[英]Trigger custom event in jQuery for use with live()

I have written a series of plugins for jQuery which essentially act as events for mobile browsers. 我已经为jQuery编写了一系列插件,这些插件实际上充当了移动浏览器的事件。 You can see them here > http://ben-major.co.uk/2011/11/jquery-mobile-events/ . 您可以在此处查看它们> http://ben-major.co.uk/2011/11/jquery-mobile-events/

Currently, they are called by $(ele).tap(handler) and so forth, but I would like to add in functionality to also fire custom events, so that the functions can be harnessed using something like $(ele).on('tap', handler); 当前,它们由$(ele).tap(handler)等调用,但我想添加功能来触发自定义事件,以便可以使用$(ele).on('tap', handler); .

I am using the following code right now, but this does not seem to work: 我现在正在使用以下代码,但这似乎不起作用:

$(function() {
    $('*').tapstart(function() { $(this).trigger('tapstart'); })
          .tapend(function() { $(this).trigger('tapend'); })
          .tap(function() { $(this).trigger('tap'); })
          .doubletap(function() { $(this).trigger('doubletap'); })
          .taphold(function() { $(this).trigger('taphold'); })
          .swipedown(function() { $(this).trigger('swipedown'); })
          .swipeup(function() { $(this).trigger('swipeup'); })
          .swipeleft(function() { $(this).trigger('swipeleft'); })
          .swiperight(function() { $(this).trigger('swiperight'); });
});

Here's a jsFiddle to demonstrate my problem. 这是一个jsFiddle来演示我的问题。 Obviously clicking on the second div should mimic the action of the first, but since it was added to the DOM after the bindings given above were parsed, it doesn't. 显然,单击第二个div应该模仿第一个div的操作,但是由于在解析了上面给出的绑定之后将其添加到DOM中,所以没有。

I guess my question is: what's the best way to achieve what I want? 我想我的问题是:实现我想要的最好的方法是什么? Is there a way to select all elements which exist in the DOM now and in the future (if possible, I would rather not use something like livequery , or an external plugin). 有没有办法选择现在和将来存在于DOM中的所有元素(如果可能的话,我宁愿不使用诸如livequery或外部插件之类的东西)。

In your case, I don't think jQuery will handle your custom events properly (as a custom event are not bubbled up to the document). 就您而言,我认为jQuery无法正确处理您的自定义事件(因为自定义事件不会冒泡到文档中)。 The answer is to bind a branch of event listeners to document . 答案是将事件侦听器的一个分支绑定到document and don't use jQuery in this situation for good. 并且不要在这种情况下永远使用jQuery。

jQuery's live mode almost do the same thing as I suggested, but it will try to match a the event.target with the bound selector (say '*' in your question,) which is very slow (also battery drainer for mobiles.) jQuery的实时模式几乎完成了我建议的操作,但是它将尝试将event.target与绑定的选择器(在您的问题中用'*'表示)相匹配,这非常慢(也消耗了手机的电量)。

If you want to interact with certain type or certain className of element, just handle it by your self, and fire desired event handlers. 如果要与元素的某些类型或某些className进行交互,只需自行处理即可,并触发所需的事件处理程序。

an example: 一个例子:

function findAncestorOfType(el, type) {
  while (el && !(el instanceof type))
    el = el.parent;
  return el;
}

document.addEventListener('click', function(evt) {
  var target = findAncestorOfType(evt.target, LIElement);
  if (target) {
    // distinguish event type
    type = 'click';
    callbacks[type](target, evt);
  }
}, false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM