亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用單獨的 .js 文件正確地將 Fancybox 初始化為 WordPress?

如何使用單獨的 .js 文件正確地將 Fancybox 初始化為 WordPress?

PHP
SMILET 2023-03-04 11:19:01
我進行了搜索,并在此處使用wp_add_inline_script. 它有效。但是,如果我想將我的初始化腳本保存到另一個 .js 文件中怎么辦?這就是我最初想要的。我怎樣才能正確加載?我試過了,但是我的加載init-fancybox一定是太早了,因為我得到了一個錯誤。(我的init-fancybox腳本不是問題,因為它在使用正確加載時有效wp_add_inline_script)function fancybox_enqueues() {     wp_register_script( 'fancybox', get_theme_file_uri( '/assets/js/jquery.fancybox.min.js' ), array('jquery'), '3.5.7', true);    wp_register_script( 'init-fancybox', get_theme_file_uri( '/assets/js/init-fancybox.js' ), array('jquery','fancybox'), '1.0', true);    wp_register_style( 'fancybox-css', get_theme_file_uri( '/css/jquery.fancybox.min.css' ), '1.0');    if ( is_singular( 'item' ) ) {        wp_enqueue_script( 'jquery');        wp_enqueue_script( 'fancybox' );        wp_enqueue_script ('init-fancybox');        wp_enqueue_style( 'fancybox-css' );    }}add_action( 'wp_enqueue_scripts', 'fancybox_enqueues');這是我得到的錯誤TypeError: $ is not a function這是init-fancybox.js$('[data-fancybox="single-item__gallery"]').fancybox({    buttons : [         "zoom",        "share",        //"slideShow",        "fullScreen",        //"download",        "thumbs",        "close"    ],    thumbs : {      autoStart : false    }  });
查看完整描述

1 回答

?
森欄

TA貢獻1810條經驗 獲得超5個贊

在您的代碼運行時,jQuery 似乎未加載。嘗試在其周圍添加以下函數以確保在代碼運行之前 jQuery 存在:


jQuery(document).ready(function ($) {


  // Your function goes here:

  $('[data-fancybox="single-item__gallery"]').fancybox({

    buttons: [

      "zoom",

      "share",

      //"slideShow",

      "fullScreen",

      //"download",

      "thumbs",

      "close"

    ],

    thumbs: {

      autoStart: false

    }

  });


});


查看完整回答
反對 回復 2023-03-04
  • 1 回答
  • 0 關注
  • 136 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號