简体   繁体   English

jQuery未在WordPress中加载

[英]jquery not loading in WordPress

I have this code... 我有这个代码...

<?php
    function add_jquery() {
       wp_enqueue_script( 'jquery' );
    }    
    add_action('init', 'add_jquery');
?>

<script type="text/javascript">
    if (typeof jQuery == 'undefined') {  
        alert('jQuery IS NOT loaded')  
    } else {
        alert('jQuery IS loaded')
    }
</script>

<!-- Let's try loading jQuery the WRONG WAY when doing it in WordPress -->
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js" charset="utf-8"></script>

<script type="text/javascript">
    if (typeof jQuery == 'undefined') {  
        alert('jQuery IS NOT loaded')  
    } else {
        alert('jQuery IS loaded')
    }
</script>

The first alert says "jQuery IS NOT loaded" and the second says "jQuery IS loaded". 第一个警告显示“未加载jQuery”,第二个警告显示“未加载jQuery”。 Why does jQuery not load when using the first method? 为什么在使用第一种方法时jQuery无法加载?

where is your add_jquery function? 您的add_jquery函数在哪里? It won't work in a wordpress template. 在wordpress模板中无法使用。 it needs to go in functions.php or someplace where it will run before the loop. 它需要进入functions.php或它将在循环之前运行的某个地方。

its because this line loads jQuery 这是因为这行加载了jQuery

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js" charset="utf-8"></script>

After this line jQuery is available. 在这行之后,jQuery可用了。

what the notes are saying this is the incorrect way to load jQuery into wordpress. 注释在说什么,这是将jQuery加载到wordpress中的错误方法。 The typical "right" way is to add a call into your header. 典型的“正确”方法是将调用添加到标题中。

If you enqueue something in wordpress - particularly jQuery, you have to enqueue everything. 如果您将某些内容放入wordpress中,尤其是jQuery,则必须将所有内容放入队列。 Otherwise your libraries will not have access to jQuery. 否则,您的库将无法访问jQuery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM