【问题标题】:If i use jQuery(document).ready(function(){ ... my code}) = undifined function如果我使用 jQuery(document).ready(function(){ ... my code}) = undefined function
【发布时间】:2020-12-15 04:48:30
【问题描述】:

我正在尝试在 wordpress ACF 自定义块中使用 owl carousel。

如果我不在脚本中输入jQuery(document).ready,猫头鹰轮播就可以工作。 但如果我有jQuery(document).ready,控制台会告诉我“jQuery (...)。OwlCarousel 不是函数”。

然后我不知道是否有两个链接 beetwen 但我有缩略图库可以单击特定图像上的启动滑块和该代码:

jQuery('.single_image_gallery').click(function(){
    mySlide = parseInt(jQuery(this).attr('data-slide'));
    jQuery(mySlider).trigger("to.owl.carousel", [mySlide, 1,true])  
})

根本不工作。

我知道 jQuery migrate for wp 5.5 存在问题,但我尝试使用插件“启用 jQuery Migrate Helper”,结果是一样的......

【问题讨论】:

  • jQuery(document).ready 遇到什么错误?你肯定想要它。可以尝试像 IIFE 一样在 jquery 中传递,(function($){ })(jQuery)
  • 您的脚本是如何入队的?您是否将 owl 添加为依赖项?

标签: javascript jquery advanced-custom-fields owl-carousel gutenberg-blocks


【解决方案1】:

尝试这样做.. 有时插件会干扰 jQuery

jQuery(function($) {
// then use it like this
 $('.single_image_gallery').click(function(){
   mySlide = parseInt($(this).attr('data-slide'));
   $(mySlider).trigger("to.owl.carousel", [mySlide, 1,true])  
 });  
});

这样你在本地导入 $ 并且它应该防止它干扰其他全局脚本或插件

【讨论】:

猜你喜欢
  • 2010-12-11
  • 1970-01-01
  • 2018-11-07
  • 1970-01-01
  • 1970-01-01
  • 2011-04-01
  • 1970-01-01
  • 2011-05-22
  • 1970-01-01
相关资源
最近更新 更多