在选择器上调用 jQuery 函数

Calling jQuery Function on Selector

本文关键字:jQuery 函数 调用 选择器      更新时间:2023-09-26

我遇到了一个问题,我试图在选择器上调用一个函数,并使用jQuery编辑/添加类到该选择器中的元素。

下面是我的代码:

(function($) {
    $(document).on('click', '.one a.button', function(){
        $('.two').loadQuestions();
    });
})(jQuery);
function loadQuestions( $ ) {
    $(this).addClass('active');
    $(this).children().addClass('transition-in');
    $(this).find('.tilt').delay(100).textillate({ 
        in: { effect: 'fadeInLeft' } 
});

它一直给我这个错误:

undefined is not a function - in regards to:
    $('.two').loadQuestions();

您正在使用loadQuestions作为插件。为此,您需要将其附加到$.fn

$.fn.loadQuestions = function(options) {
    $(this).addClass('active');
    $(this).children().addClass('transition-in');
    $(this).find('.tilt').delay(100).textillate({ in: { effect: 'fadeInLeft' } });
    return this; // important
});

或将其用作正常功能

function loadQuestions(that) {
    that.addClass('active');
    that.children().addClass('transition-in');
    that.find('.tilt').delay(100).textillate({ in: { effect: 'fadeInLeft' }}); 
});
loadQuestions($('.two')); // call it