繁体   English   中英

jQuery在Wordpress中不起作用

[英]Jquery wont work in wordpress

这可能是一个很容易回答的问题,但是无论遵循什么教程,我的Jquery都不会对我的主题起作用。

我的js文件位于/js/jquery.js,其中的代码仅用于测试目的:

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


$('#banner').hide();
});

我的functions.php文件与索引位于相同的位置,为此而完成的代码为:

    <?php
function add_my_script() {
    wp_enqueue_script(
        'jquery', // name your script so that you can attach other scripts and de-register, etc.
        get_template_directory_uri() . 'js/jquery.js', // this is the location of your script file
        array('jquery') // this array lists the scripts upon which your script depends
    );
}
add_action( 'wp_enqueue_scripts', 'add_my_script' );
?>

而且什么也没发生...有人可以纠正我做错的事情吗? 这就是让我不喜欢使用wordpress的原因。

您可能需要在“ js”之前加一个斜杠:

get_template_directory_uri() . '/js/jquery.js'

话虽如此,您可以使用浏览器开发人员工具中的网络监视器来检查站点。 它会告诉您正在获取哪些URL。 或者,也许只是检查开发人员工具的控制台。 如果无法获取您的jQuery文件,则可能会显示404错误。

wp_enqueue_script(
    'call this something else!', // name your script so that you can attach other scripts and de-register, etc.
    get_template_directory_uri() . '/js/jquery.js', // this is the location of your script file
    array('jquery') // this array lists the scripts upon which your script depends
);

您有一些错别字.....还通过在chrome或firefox中使用dev.console来检查jquery是否可用(右键单击页面,单击inspect元素,然后进行控制台),您将收到错误的jquery错误或类似的。

同样在您的前端页面中:

get_header();
get_footer();

您不要使用head标记,因为这些功能会提取头文件和页脚文件。 没有get_header()和get_footer-您会遇到问题:)

暂无
暂无

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

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