简体   繁体   English

设置html()后触发自定义事件

[英]Trigger custom event after html() set

I have a response handler that replaces content from ajax response. 我有一个响应处理程序,可替换ajax响应中的内容。 I want to fire an event after the content is replaced. 我想在替换内容后触发一个事件。 This is the response handler: 这是响应处理程序:

function(response)
    {
        /* animate to top if called from bottom pagination */
        if ( caller === 'pag-bottom' && jq('#subnav').length ) {
            var top = jq('#subnav').parent();
            jq('html,body').animate({scrollTop: top.offset().top}, 'slow', function() {
                jq(target).fadeOut( 100, function() {
                    jq(this).html(response);
                    jq(this).fadeIn(100);
                });
            });

        } else {
            jq(target).fadeOut( 100, function() {
                jq(this).html(response);
                jq(this).fadeIn(100);
            });
        }
        jq('.item-list-tabs li.selected').removeClass('loading');
     // the event I want to trigger
        jq( document ).trigger( "buddyFilter:replaced", [ object, filter] );
    }

My problem is this event fires to early so my code that fires on event doesn't calculate the size of the div correctly and formatting is incorrect. 我的问题是此事件触发得很早,所以我在事件上触发的代码无法正确计算div的大小,并且格式不正确。

I have tried various combinations of trying to chain the event trigger but I can't get it right. 我尝试了各种尝试链接事件触发器的组合,但我做对了。 I can't use a manual delay because then that will cause visual jump and not always work if loading time is slower than time. 我不能使用手动延迟,因为那样会导致视觉跳动,并且如果加载时间比时间慢的话,也不会总是起作用。

How can I trigger this event the moment the html() value has rendered? 呈现html()值后,如何触发此事件?

Edit: 编辑:

Here is jsfiddle that is similar to my code but I had to change it about to get to work in fiddle. 这是与我的代码相似的jsfiddle,但是我不得不对其进行更改以使其能够在小提琴中工作。 It shows that right after the event fires the div is 0 height 它显示事件触发后,div高度为0

http://jsfiddle.net/jq7n4kfL/1/ http://jsfiddle.net/jq7n4kfL/1/

Try utilizing .promise() , .then() 尝试利用.promise() .then()

    function success(response)
        {
            /* animate to top if called from bottom pagination */
            if ( caller === 'pag-bottom' && jq('#subnav').length ) {
                var top = jq('#subnav').parent();
                return jq('html,body').animate({scrollTop: top.offset().top}, 'slow', function() {
                    jq(target).fadeOut( 100, function() {
                        jq(this).html(response);
                        jq(this).fadeIn(100);
                        jq('.item-list-tabs li.selected').removeClass('loading');
                    });
                }).promise();

            } else {
               return jq(target).fadeOut( 100, function() {
                    jq(this).html(response);
                    jq(this).fadeIn(100);
                    jq('.item-list-tabs li.selected').removeClass('loading');
                }).promise();
            }             
        };

$.ajax().then(success).then(function() {
  // the event I want to trigger
  jq( document ).trigger( "buddyFilter:replaced", [ object, filter] );
}]);

You're triggering the event too early. 您触发事件太早了。 Try this: 尝试这个:

            jq(target).fadeOut( 100, function() {
                jq(this).html(response);
                jq(this).fadeIn(100, function() {
                   //trigger the event herer
                });
            });

Of course, you have to do it in two places. 当然,您必须在两个地方进行。

You just put the event firing right after the actual HTML replacement takes place: 您只需在实际替换HTML之后立即触发事件:

jq(target).fadeOut( 100, function() {
    jq(this).html(response);
    jq( document ).trigger( "buddyFilter:replaced", [ object, filter] );
    jq(this).fadeIn(100);
});

Also note that you repeat exactly the same code twice so better DRY it by wrapping it in a method. 还要注意,您重复两次完全相同的代码,因此最好将其包装在方法中以使其干燥。

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

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