javascript 未捕获的类型错误:未定义不是 Wordpress 中的函数(匿名函数)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24763256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:19:35  来源:igfitidea点击:

Uncaught TypeError: undefined is not a function (anonymous function) in Wordpress

javascriptjquerywordpress

提问by Mythbuster

I am getting the following error, which appears to be Javascript not interpreting the $ symbol.

我收到以下错误,这似乎是 Javascript 没有解释 $ 符号。

Uncaught TypeError: undefined is not a function main.js:1 (anonymous function) main.js:1

Uncaught TypeError: undefined is not a function main.js:1 (anonymous function) main.js:1

Appended below is the main.js code. This was working fine sometime back. I am trying to find out pointers to where to look for the issues, i.e. theme, jquery imports, etc. Any suggestions are welcome.

下面附加的是 main.js 代码。这在某个时候工作得很好。我试图找出指向何处查找问题的指针,即主题、jquery 导入等。欢迎提出任何建议。

$(function(){ 
var cardHeight = 0;

function _setCardHeight(){

    $(".subpage-box").each( function(){ var current_height = 
    $(this).height();

        cardHeight = ( current_height > cardHeight) ? current_height : 
        cardHeight;

    }); $(".subpage-box").each( function(){ if( $(this).height() < 
    cardHeight ){ $(this).height( cardHeight );   } });


}

function _setNavStyle(){
    $("menu-main-menu > li > a").each( function(){
        var text = $(this).html();

        if( $(this).contains("for") ){

        }
    });
}

_setNavStyle();
_setCardHeight();

});

回答by adeneo

Wordpress is running in noConflict mode by default, change the DOM ready wrapping to

Wordpress 默认以 noConflict 模式运行,将 DOM 就绪包装更改为

jQuery(function($){ 

    // your code goes here

});

otherwise $ will be undefined

否则 $ 将是未定义的

Da Codex

大法典