简体   繁体   English

无法在 WordPress 子主题中使用 jQuery

[英]Unable to use jQuery in WordPress Child Theme

I can't figure out how to use jQuery in my WordPress child theme scripts.我不知道如何在我的 WordPress 子主题脚本中使用 jQuery。 From my research on SO , I have enqueued my external JavaScript file and have tried coding the jQuery commands in NoConflict mode with no success.从我对 SO 的研究来看,我已经将我的外部 JavaScript 文件排入队列,并尝试在 NoConflict 模式下对 jQuery 命令进行编码,但没有成功。 Here is the barebones template I'm experimenting on right now (it is essentially a blank page.php and I added a button and paragraph at the bottom):这是我现在正在试验的准系统模板(它本质上是一个空白的 page.php,我在底部添加了一个按钮和段落):

<?php
/**
 *
 * Template Name: My JQ Template
 *
 * @package GeneratePress
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

get_header(); ?>

<?php
    global $wpdb;

    $course_result = $wpdb->get_results('SELECT * FROM SC_COURSES ORDER BY COURSE_NAME');
?>

<div id="primary" <?php //generate_content_class();?>>

</div>

<main id="main" <?php generate_main_class(); // Page title and content ?>> 
    <?php
    do_action( 'generate_before_main_content' );

    while ( have_posts() ) : the_post();
        get_template_part( 'content', 'page' );
        // If comments are open or we have at least one comment, load up the comment template.
        if ( comments_open() || '0' != get_comments_number() ) : ?>
            <div class="comments-area">
                <?php comments_template(); ?>
            </div>
        <?php endif;
    endwhile;

    do_action( 'generate_after_main_content' );
    ?>
    <br>    
    <div class="container">
        <div>       
            <input id="testbutton" type="button" onClick="myFunction()" value="POP!">
            <p>This is a test</p>
        </div>  

    </div><!-- container -->

</main><!-- #main -->

    <?php
    do_action( 'generate_after_primary_content_area' );

    get_footer();
    ?>

In functions.php, I have the following:在functions.php中,我有以下内容:

<?php
function enqueue_child_theme_styles() {
    if ( is_page_template( 'jqtemplate.php' ) ) {
        //deregister the parent bootstrap style and script  
        wp_deregister_style( 'bootstrap' );
        wp_deregister_script( 'bootstrap' );
        wp_deregister_script( 'mycustomjs' );

        //enqueue my child theme stylesheet
        wp_enqueue_style( 'child-style', get_stylesheet_uri(), array('theme') );

        //enqueue bootstrap in the child theme 
        wp_enqueue_script('bootstrap-js', get_stylesheet_directory_uri().'/bootstrap/js/bootstrap.min.js', array('jquery'), NULL, true);
        wp_enqueue_style('bootstrap-css', get_stylesheet_directory_uri().'/bootstrap/css/bootstrap.min.css', false, NULL, 'all');

        //Enqueue the custom jQuery specific scripts
        wp_enqueue_script('mycustomjs', get_stylesheet_directory_uri().'/js/mycustomjs.js', array('jquery'), NULL, true);
    }
}

add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles', PHP_INT_MAX); 

Within mycustomjs.js, I have the following:在 mycustomjs.js 中,我有以下内容:

jquery(document).ready(function(){
    jquery("p").mouseenter(function(){
        jquery("p").text("This is some new text");
    });
}); 


function myFunction() {
    var txt;
    if (confirm("Press a button, mkay!")) {
        txt = "You pressed OK!";
    } else {
        txt = "You pressed Cancel!";
    }
    document.getElementById("testbutton").value = txt;
}

I am positive the script is loading, because that little demo with changing the button text works.我肯定脚本正在加载,因为更改按钮文本的小演示有效。 I know the Bootstrap styles and scripts are loading because in the production version they are visible.我知道 Bootstrap 样式和脚本正在加载,因为在生产版本中它们是可见的。

I can't make jQuery do anything, though.不过,我不能让 jQuery 做任何事情。 I have tried adding var j = jQuery.noConflict();我试过添加var j = jQuery.noConflict(); and then using j instead of jquery as described here , I have changed jquery to jQuery, and I have tried putting the jQuery lines directly into the jqtemplate.php header with no success.然后使用j个,而不是如所描述的jquery 这里,我已经改变jQuery来jQuery和我试图直接把jQuery的线与没有成功的jqtemplate.php报头。

I know WordPress includes jQuery, so what am I missing here?我知道 WordPress 包含 jQuery,那么我在这里错过了什么?

WordPress includes jQuery indeed. WordPress 确实包含 jQuery。 But it still has to be loaded .但它仍然必须加载

After loading jQuery you just have to make sure that you use jQuery instead of jquery .加载 jQuery 后,您只需确保使用jQuery而不是jquery like this:像这样:

jQuery( document ).ready( function() {

    alert( 'test to see if this is working' );

    jQuery("p").mouseenter( function() {

        jQuery("p").text("This is some new text"); 
    }); 
});

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

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