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

问题描述

这应该很容易吧?然而,我无法在任何地方找到此类功能的任何示例.问题是,在我执行 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 函数以接受回调函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-09 13:58