weixin_33744141 2016-08-05 09:57 采纳率: 0%
浏览 40

jQuery停止/开始传播

So I have seen a few questions on this matter but none have been applicable to my situation.

As an example, I have a web page that has multiple Javascript files that attach handlers to controls on the page - some of the controls can have multiple handlers from different JS files e.g. A dropdown list has 2 change events. The problem is that the second change function runs before an AJAX request on the first change event has completed.

What I need to do is to run an ajax request on the first change event and IF THAT IS SUCCESSFUL then run the second event. In the past I could have used

async: false

in the ajax request but that has been deprecated.

My hope was to, on the first change event, call

event.stopImmediatePropagation()

and then start it back up again after the ajax request has run. As far as I can see this is not an option.

Then I tried stopping propagation on the first change, before the AJAX is run, and trying to trigger the change event after the AJAX is run, on the control but that causes an infinite loop.

So:

Is there any way to Pause Propagation?

Is there any way to store event handlers for a control and then trigger them later?

Thanks.

  • 写回答

1条回答 默认 最新

  • weixin_33743661 2016-08-05 10:15
    关注

    event.stopPropagation() Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.

    This method does not accept any arguments.

    We can use event.isPropagationStopped() to determine if this method was ever called (on that event object).

    This method works for custom events triggered with trigger() as well.

    Note that this will not prevent other handlers on the same element from running.

    Example: Kill the bubbling on the click event.

    $( "p" ).click(function( event ) {
      event.stopPropagation();
      // Do something
    }); 
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!