我在我的 WordPress 網站上遇到了這個錯誤“Uncaught TypeError: $ is not a function at (index):2”我找到了這個解決方案來添加var $ = jQuery.noConflict();之前$( document ).ready(function(){但是,我在網站的主題文件夾中找不到“$( document ).ready(function(){”代碼來修復它。這是通過 Chrome 瀏覽器的檢查、源窗口獲得的確切代碼<html class="no-js" itemscope itemtype="https://schema.org/WebPage"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /><link rel="alternate" hreflang="en" href="https://ccsi.global/"/><link rel="shortcut icon" href="https://ccsi.global/wp-content/uploads/2018/05/imageedit_89_3942571748.png" type="image/x-icon" /><link type="text/css" media="all" href="https://ccsi.global/wp-content/cache/autoptimize/css/autoptimize_9668264b3a77ccf7dc30b539d4038dbc.css" rel="stylesheet" /><title>Comcapint – Home – Compliance and Capacity Skills International</title><link rel='dns-prefetch' href='//fonts.googleapis.com' /><link rel='dns-prefetch' href='//s.w.org' /><link rel="alternate" type="application/rss+xml" title="Comcapint » Feed" href="https://ccsi.global/feed/" /><link rel="alternate" type="application/rss+xml" title="Comcapint » Comments Feed" href="https://ccsi.global/comments/feed/" /> <script type="text/javascript">window._wpemojiSettings = {"baseUrl":"https:\/\/s.w.org\/images\/core\/emoji\/13.0.0\/72x72\/","ext":".png","svgUrl":"https:\/\/s.w.org\/images\/core\/emoji\/13.0.0\/svg\/","svgExt":".svg","source":{"concatemoji":"https:\/\/ccsi.global\/wp-includes\/js\/wp-emoji-release.min.js?ver=5.5"}};$(".nav-tabs a").click(function(){ $(this).tab('show');});$('.nav-tabs a').on('shown.bs.tab', function(event){ var x = $(event.target).text(); // active tab var y = $(event.relatedTarget).text(); // previous tab $(".act span").text(x); $(".prev span").text(y);});});
1 回答

一只甜甜圈
TA貢獻1836條經驗 獲得超5個贊
大多數 WordPress 模板已經調用 noConflict()
當您想$在代碼中使用時,最好使用 IIFE 包裝器
(function($){
// can use $ here
$(document).ready....
})(jQuery);
jQuery還要確保您在加載庫之前沒有嘗試使用。使用wp_enqueue_script()將解決這個問題
- 1 回答
- 0 關注
- 125 瀏覽
添加回答
舉報
0/150
提交
取消