Javascript 未捕获的TypeError:$(…)。另一个Func不是函数
flip.js效果正常,flip:done内的console.log也正常,但名为inside flip:done的“anotherfunc”不正常。请帮忙Javascript 未捕获的TypeError:$(…)。另一个Func不是函数,javascript,jquery,Javascript,Jquery,flip.js效果正常,flip:done内的console.log也正常,但名为inside flip:done的“anotherfunc”不正常。请帮忙 function anotherfunc( ) { console.log("its anotherfunc"); } jQuery(document).ready(function($) { //it is working here but not inside flip:done anotherfunc(); $(".c
function anotherfunc( ) {
console.log("its anotherfunc");
}
jQuery(document).ready(function($) {
//it is working here but not inside flip:done
anotherfunc();
$(".card").flip({
trigger: 'hover',
speed : 1000
});
$(".card").on('flip:done',function( ){
// this is also working
console.log('from inside flip:done');
// this is also working
console.log( $(this) );
// this is saying "is not a function"
$(this).anotherfunc();
});
});//on laod
是的,anotherfunc是一个函数,但是没有调用
$(this)
如果要向其传递对象,则在顶部作用域上声明anotherfunc()
function anotherfunc(myobject) {
....
}
并将其命名为$(this)
请确保清除myobject是否允许为null您可以将其称为
anotherfunc($(this));
或者像这样声明:
(function ($) {
$.fn.anotherfunc = function () {
// some code
}
})(jQuery);
$(this).anotherfunc();
然后你可以像这样使用它:
(function ($) {
$.fn.anotherfunc = function () {
// some code
}
})(jQuery);
$(this).anotherfunc();
看看这个演示:这是因为
$(this)
指的是$(“.card”)。在('flip:done',function(){..}
查看javascript范围,并确认它是这样声明的。$.fn.anotherfunc=function(){//some code}感谢它是这样工作的。$.fn.anotherfunc=function(){//some code}很好。你应该选择并投票支持Ashkan的答案。