callback for .trigger function

前端 未结 4 1060
北海茫月
北海茫月 2021-01-29 03:54

I have the following code:

 $(\"#scheduleLink\").trigger(\"click\");
 alert(\"text\")

This is the click handler:

$(\"#scheduleL         


        
4条回答
  •  南笙
    南笙 (楼主)
    2021-01-29 04:47

    Looks like there are 3 solutions:

    1 - Create a function, and invoke it from a callback on .load

    function loadComplete() {
        alert("text");
    }
    
    $("#scheduleLink")
    .on("click", function () {
    
        $("#publicationBlockContent")
        .load("/my-url", function () {
            loadComplete();
        });
    
    })
    .trigger("click");
    

    2 - Bind a custom event to $("#scheduleLink") called "loadComplete", and trigger it from a callback on .load

    $("#scheduleLink")
    .on("loadComplete", function () {
        alert("text");
    })
    .on("click", function () {
    
        $("#publicationBlockContent")
        .load("/my-url", function () {
            $("#scheduleLink").trigger("loadComplete");
        });
    
    })
    .trigger("click");
    

    3 - If you don't need a .load, you can use the promise object supported by $.ajax, $.get and $.post

    $("#scheduleLink")
    .on("click", function () {
    
        return $.get("/my-url", function () {
            $("#scheduleLink").trigger("loadComplete");
        });
    
    })
    .trigger("click");
    
    var promise = $("#scheduleLink").triggerHandler("click");
    promise && promise.done(function () {
        alert("click");
    });
    

提交回复
热议问题