dongxing1965 2015-02-13 20:29
浏览 58
已采纳

WordPress没有调用默认的jQuery文件

I'm trying to call jQuery DataTable JS file for my plugin that uses DataTable to display the query from the database. The JS file is stored locally on the server.

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

The WordPress default jQuery file is not getting called. When I register and call the jQuery file hosted on Google, it seems to work. Is there a reason why this is happening?

Here is what I have so far:

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:

<?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>
  • 写回答

2条回答 默认 最新

  • doulai1910 2015-02-23 17:25
    关注

    The following change fixed the issue:

    Changing the jQuery in index.php from:

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

    to the following:

    <script>
        jQuery(document).ready(function() {
            $('#example').DataTable();
        });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示