【问题标题】:How to get the source element of PrimeFaces pfAjaxStart event in jQuery如何在jQuery中获取PrimeFaces pfAjaxStart事件的源元素
【发布时间】:2015-09-10 12:31:10
【问题描述】:

我有一个用于 pfAjaxStart 事件的自定义 jQuery 事件处理程序。我想仅为来自具有特定 css 类的元素的事件注册(或在内部检查)此处理程序,例如处理 ajax 启动。我怎样才能做到这一点。

$(document).on("pfAjaxStart", function(event, xhr, options) {
    /*
     * How to perform this only for events by elements with class handle-ajax-start???
     */
    console.log("Ajax Started...");
});

【问题讨论】:

  • 事件目标始终与文档相关,因此无法知道是谁真正触发了ajax事件it's going to be always the document。
  • 只有全局处理程序有什么好处?
  • 在内部使用 p:ajaxStatus。对于用户来说,这只是一个 ajax 事件即将开始的标志。在其他情况下,例如here ajax 事件由组件(作为目标)显式触发,同时在另一种情况下,它代表文档触发(由设计),因此您可以在文档上监听(全局)。

标签: javascript jquery jsf primefaces


【解决方案1】:

使用pfAjaxStart,您最接近的赌注是document.activeElement。

$(document).on("pfAjaxStart", function(event) {
    var $source = $(document.activeElement);
    // ...
});

鉴于您还指定了 xhr 和 options,它们实际上在 pfAjaxStart 期间不存在(并且在上面已修复),我相信您实际上打算挂钩 @987654328 @。然后可以通过options.source 获得 DOM 中源元素的 ID。这可能比document.activeElement 更可靠。

$(document).on("pfAjaxSend", function(event, xhr, options) {
    var $source = $(document.getElementById(options.source));
    // ...
});

【讨论】:

  • 根据我的经验,activeElement 并不总是准确的。至于 pfAjaxSend 它肯定会起作用,但在某些情况下为时已晚,例如取消事件。这取决于OP的要求。但是是的,这是他能得到的最接近的东西...... :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-29
  • 2012-04-20
  • 2017-07-09
  • 2011-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多