简体   繁体   English

jQuery ajaxSend-哪个元素触发了ajax?

[英]jQuery ajaxSend - which element has triggered ajax?

I'm working on a rather large project, and I would like to find which DOM element has triggered ajax call. 我正在做一个相当大的项目,我想找到哪个DOM元素触发了ajax调用。

I've such code: 我有这样的代码:

initAjaxSend: ->
    that = @
    $(document).ajaxSend (e, xhr, options)->
      if typeof event != 'undefined' && event
        e = event
      that.showPreloader(e, xhr)

than showPreloader() function use e.target to find a DOM element. showPreloader()函数使用e.target来查找DOM元素。

It works fine only in Chrome, because Chrome has global event variable. 由于Chrome具有全局event变量,因此只能在Chrome中正常运行。 Any ideas how to make it work in other browsers? 有什么想法如何使其在其他浏览器中起作用?

You are mixing, the global event and the event e that is created when your function is fired. 您正在混合全局event和触发函数时创建的事件e What you are looking for can be found in e which is the triggered event, under e.delegateTarget , you read more about event.delegateTarget 您要查找的内容可以在触发事件的e中找到,在e.delegateTarget下,您可以了解有关event.delegateTarget的更多信息

Quoting from docs: "For non-delegated event handlers attached directly to an element, event.delegateTarget will always be equal to event.currentTarget ." 引自文档:“对于直接附加到元素的未委托事件处理程序, event.delegateTarget始终等于event.currentTarget 。”

initAjaxSend: ->
    that = @
    $(document).ajaxSend (e, xhr, options) ->
        console.log e.delegateTarget   
        that.showPreloader(e, xhr)

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

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