简体   繁体   English

WordPress没有调用默认的jQuery文件

[英]WordPress is not calling the default jQuery file

I'm trying to call jQuery DataTable JS file for my plugin that uses DataTable to display the query from the database. 我正在尝试为我的插件调用jQuery DataTable JS文件,该插件使用DataTable从数据库中显示查询。 The JS file is stored locally on the server. JS文件本地存储在服务器上。

WordPress: v4.0.1 jQuery: v1.11.1 DataTable: v1.10.5 WordPress:v4.0.1 jQuery:v1.11.1 DataTable:v1.10.5

The WordPress default jQuery file is not getting called. WordPress默认的jQuery文件没有被调用。 When I register and call the jQuery file hosted on Google, it seems to work. 当我注册并调用托管在Google上的jQuery文件时,它似乎有效。 Is there a reason why this is happening? 有没有理由发生这种情况?

Here is what I have so far: 这是我到目前为止:

functions.php: 的functions.php:

function load_jquery_datatables() {
    wp_enqueue_script('jquery');
    wp_register_script( 'jquery-datatables', plugins_url('js/jquery.dataTables.min.js'), array('jquery'));
    wp_enqueue_script('jquery-datatables');
}

index.php: index.php文件:

<?php add_action('wp_enqueue_scripts', 'load_jquery_datatables');?>

<script>
    $(document).ready(function() {
        $('#example').DataTable();
    });
</script>

<table id="example" class="display">
            <form>
            <thead>
                <tr>
                    <td style="text-align: center;"><input type="checkbox" id="selectall"></td>
                    <?php
                    foreach ( $wpdb->get_col( "DESC " . $table_name, 0 ) as $column_name ) 
                    {
                        echo '<th style="text-align: center;">' . $column_name . '</th>';
                    }
                    ?>
                </tr>
            </thead>

            <tfoot>
                <tr>
                    <td style="text-align: center;"><input type="checkbox" id="selectall"></td>
                    <?php
                    foreach ( $wpdb->get_col( "DESC " . $table_name, 0 ) as $column_name ) 
                    {
                        echo '<th style="text-align: center;">' . $column_name . '</th>';
                    }
                    ?>
                </tr>
            </tfoot>
            <tbody>
                <?php
                foreach ( $results as $row ) 
                {
                    echo '<tr>';
                        echo '<td><input type=\'checkbox\' class=\'check\' value=' . $id . '></td>';
                        echo '<td>' . $row->id . '</td>';
                        echo '<td>' . $row->status . '</td>';
                        echo '<td>' . $row->create_time . '</td>';
                        echo '<td>' . $row->start_time . '</td>';
                        echo '<td>' . $row->duration . '</td>';
                        echo '<td>' . $row->source . '</td>';
                        echo '<td>' . $row->source_id . '</td>';
                    echo '</tr>';
                }
                    ?>
            </tbody>
</form>
    </table>

It's been awhile since I have created a WordPress theme but I always used this method and it worked great! 自从我创建了一个WordPress主题以来,我已经有一段时间了,但我总是使用这种方法而且效果很好!

FUNCTIONS.PHP 的functions.php

function custom_function_name() {
     wp_enqueue_script("jquery");
     wp_head(); ?>
     <script type="text/javascript" src="<?php bloginfo("template_url"); ?>/js/yourScript.js"></script><?php 
}

Call to action in your HEADER.PHP NOT INDEX.PHP 在您的HEADER.PHP NOT INDEX.PHP中采取行动

<head>
     // Link stylesheets and page titles
     // Call your function
     <?php custom_function_name(); ?>
     <script>
         $(document).ready(function() {
             $('#example').DataTable();
         });
     </script>
</head>

The following change fixed the issue: 以下更改解决了该问题:

Changing the jQuery in index.php from: 在index.php中更改jQuery:

<script>
    $(document).ready(function() {
        $('#example').DataTable();
    });
</script>

to the following: 以下内容:

<script>
    jQuery(document).ready(function() {
        $('#example').DataTable();
    });
</script>

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

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