JQuery - 使用.on和元素插入

Pet*_*ada 6 javascript jquery

我有一个功能,可以为特定对象创建工具提示.目前,我在ajax插入后运行工具提示函数来创建和追加新的工具提示对象.我很好奇是否有办法使用.on()在插入时自动运行工具提示功能,而不是手动运行它.

例如:

 $('[title]').on('inserted', function(){
     tooltip(this);
 });
Run Code Online (Sandbox Code Playgroud)

我做了一些阅读,它看起来像自定义触发器可能是要走的路,但我喜欢它,如果它存在这样的东西:)

inh*_*han 1

这是根据请求的伪代码。

$(document).ready(function() {
    $('body').on('added','*',function() {
        console.log($(this),'has been added');
    });
    $('body').append('<div>This is the first div</div>');
});

(function($) {
    fncs = {
        append:$.fn.append,
        appendTo:$.fn.appendTo
        // etc.
    }
    // we're assigning the original functions in this
    // object to be executed (applied) later
    $.fn.append = function() {
        fncs.append.apply(this,arguments);
        $(this).children().last().trigger('added');
        return $(this);
    }
    $.fn.appendTo = function() {
        fncs.appendTo.apply(this,arguments);
        return $(this);
        // no need to trigger because this function calls the one
        // above for some reason, and it's taking care of the
        // triggering the right element(s I think)
    }
})(jQuery);
Run Code Online (Sandbox Code Playgroud)