本文介绍了如何扩展jQuery的replaceWith函数来接受回调函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该很容易,对吧?但我不能sem在任何地方找到这样的功能的任何示例。问题是,在我做一个replaceWith()之后,我想对那些写入DOM的元素做一些事情,但如果我尝试在replaceWith()调用之后对它们做一些事情,他们不存在我需要确保replaceWith()完全完成。我只想让这样的工作:

This should be easy, right? Yet I can't sem to find any examples of such a functionality anywhere. The problem is that after I do a replaceWith() I want to then do something with those elements that were written to the DOM, but if I try to do something to them right after the replaceWith() call they don't exist yet so I need to be sure the replaceWith() is completely finished. I just want something like this to work:

$('#foo').replaceWith('some text', function() {
     //do something else here
});

想法?

推荐答案

您可以自己调用 replaceWith 的函数:

You can make your own function that calls replaceWith:

$.fn.replaceWithCallback = function(replace, callback){
    var ret = $.fn.replaceWith.call(this, replace); // Call replaceWith
    if(typeof callback === 'function'){
       callback.call(ret); // Call your callback
     }
    return ret;  // For chaining
};

这篇关于如何扩展jQuery的replaceWith函数来接受回调函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 22:11