我遇到一个问题,我试图在选择器上调用函数,并使用 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
关于javascript - 在选择器上调用 jQuery 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27420060/