简体   繁体   English

在WordPress中找不到jQuery文件

[英]Not found jquery file in wordpress

I linked jquery file in functions.php, but doesn't work Here is code in functions.php 我在functions.php中链接了jquery文件,但是不起作用这是functions.php中的代码

add_action("wp_footer","js_scripts");
function js_scripts()
{
    wp_enqueue_script("jquery",get_template_directory_uri()."/jquery.js");
    wp_enqueue_script("f",get_template_directory_uri()."/f.js");
}

Here is code in f.js 这是f.js中的代码

$(document).ready(function(){
    alert("ready good");
});

You need to make sure you have uploaded your f.js file in /wp-content/themes/youractivetheme/f.js 您需要确保已将您的f.js文件上传到/wp-content/themes/youractivetheme/f.js

And also don't need to upload jquery.js because it already inherits and include from wp-includes. 并且也不需要上传jquery.js,因为它已经从wp-includes继承并包含了。

You can use this method instead of get_template_directory_uri() 您可以使用此方法代替get_template_directory_uri()

get_stylesheet_directory_uri(); get_stylesheet_directory_uri();

Or 要么

function wpdocs_scripts_method() {
wp_enqueue_script( 'custom-script', get_stylesheet_directory_uri() . '/f.js', array( 'jquery' ) ); } 
add_action( 'wp_enqueue_scripts', 'wpdocs_scripts_method', 100 );

you add like this and define priority hight so it would be load at last. 您可以像这样添加并定义高优先级,以便最后加载它。

Firstly, ensure you have WordPress loading onto your template. 首先,请确保将WordPress加载到模板上。

In your head file, ensure you have this loaded in not in functions file 在头文件中,确保已将此文件加载到不在功能文件中

<?php wp_enqueue_script("jquery"); ?>

Next, WordPress has to use noConflict mode or use the base jQuery call instead of $. 接下来,WordPress必须使用noConflict模式或使用基本jQuery调用而不是$.

Source 资源

There are few problems with your code... 您的代码几乎没有问题...

First of all , you should not enqueue scripts using wp_footer hook. 首先 ,您不应使用wp_footer钩子使脚本wp_footer队。 It's way to late. 太晚了。

wp_footer is an action hook that is run during wp_footer() function call. wp_footer是在wp_footer()函数调用期间运行的动作挂钩。 And printing footer scripts is an action assigned to that hook. 打印页脚脚本是分配给该挂钩的操作。 It means that to print any scripts in there, you have to enqueue them before they should get printed. 这意味着要在其中打印任何脚本,必须先将它们放入队列,然后才能打印它们。

The good practice is to enqueue scripts using wp_enqueue_scripts hook. 好的做法是使用wp_enqueue_scripts钩子使脚本入队。 And if you want to enqueue it for footer, then there's a param telling WP if given script should be placed in header or footer. 而且,如果您希望将其排入页脚,则有一个参数告诉WP是否应将给定脚本放置在页眉或页脚中。 So after changing this, your code would look like this: 因此,更改此代码后,您的代码将如下所示:

function js_scripts() {
    wp_enqueue_script( 'jquery', get_template_directory_uri() . '/jquery.js', array(), null, true );
    wp_enqueue_script( 'f', get_template_directory_uri() . '/f.js', array(), null, true );
}
add_action( 'wp_enqueue_scripts', 'js_scripts' );

Another thing is that WordPress already has its own jQuery file and you should always use that one. 另一件事是WordPress已经拥有自己的jQuery文件,您应该始终使用该文件。 All themes and plugins assume that this is the version of jQuery that is used on the site - so enqueueing your own jQuery file may cause a lot of conflicts. 所有主题和插件都假定这是该站点上使用的jQuery版本-因此将自己的jQuery文件放入队列可能会导致很多冲突。 And you don't have to enqueue it - all you need to do is to use script dependency mechanism: 而且您不必排队-您所需要做的就是使用脚本依赖机制:

function js_scripts() {
    wp_enqueue_script( 'f', get_template_directory_uri() . '/f.js', array('jquery'), null, true );
}
add_action( 'wp_enqueue_scripts', 'js_scripts' );

And last thing ... WordPress uses jQuery in no conflict mode. 最后一件事 ... WordPress在无冲突模式下使用jQuery。 It means that you can't use $ in your scripts, because no such global variable is defined. 这意味着您不能在脚本中使用$ ,因为未定义此类全局变量。 You have to use jQuery instead. 您必须改用jQuery。 You may make your life easier by wrapping all your code with a function. 通过使用功能包装所有代码,可以使生活更轻松。 So your JS file should look like this: 因此,您的JS文件应如下所示:

jQuery(function ($) {
    // you can use $ inside this function

    $(document).ready(function(){
        alert("ready good");
    });
});

Jquery is already in your WordPress, you just enqueue it into your file. jQuery已经在您的WordPress中,您只需将其排队到文件中即可。 The way you can use it as given below : 您可以按照以下方式使用它:

function theme_enqueue_scripts() {
wp_enqueue_script( 'f-script', get_stylesheet_directory_uri() . '/f.js', array( 'jquery' ) ); } 
add_action( 'wp_enqueue_scripts', 'theme_enqueue_scripts', 10 );

So the f.js file will enqueue the jquery before it loads as 'jquery' has given as dependency of that handle. 因此f.js文件将在加载jquery之前使其入队,因为“ jquery”已作为该句柄的依赖项给出。

Hope it will work for you. 希望它对您有用。

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

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