dragon321723 2016-11-20 13:36
浏览 91

在Wordpress中包含jquery UI

I have tried to get jquery-ui working with wordpress with no luck. Can someone point me in the right direction? I've dug up a ton of SO resources and blogs explaining it, but so far can't get this working. However when I include the scripts directly in the page itself I can get the datepicker to function properly. I'm trying to make the table rows draggable (and eventually droppable).

Here is what I currently have-

In /themes/twentysixteen/functions.php

I followed the steps here and enqueued jquery-ui functions Correctly enqueue jquery-ui. I then added a couple of my own scripts based on the information here Add jquery scripts

/* Used to include jquery-ui scripts
*  Taken from http://xl-websites.com/how-to-enqueue-jquery-or-jquery-ui-correctly-in-wordpress/
*
*/

function add_jquery_ui() {
        wp_enqueue_script('jquery-ui-core');
        wp_enqueue_script('jquery-ui-widget');
        wp_enqueue_script('jquery-ui-mouse');
        wp_enqueue_script('jquery-ui-accordion');
        wp_enqueue_script('jquery-ui-autocomplete');
        wp_enqueue_script('jquery-ui-slider');
        wp_enqueue_script('jquery-ui-tabs');
        wp_enqueue_script('jquery-ui-sortable');
        wp_enqueue_script('jquery-ui-draggable');
        wp_enqueue_script('jquery-ui-droppable');
        wp_enqueue_script('jquery-ui-datepicker');
        wp_enqueue_script('jquery-ui-resize');
        wp_enqueue_script('jquery-ui-dialog');
        wp_enqueue_script('jquery-ui-button');
}
add_action( 'wp_enqueue_scripts', 'add_jquery_ui' );

function custom_js() {
        wp_enqueue_script('date', get_template_directory_uri() . '/js/date.js', array('jquery'), false, true);
        wp_enqueue_script('drag', get_template_directory_uri() . '/js/drag.js', array('jquery'), false, true);
}
add_action('wp_enqueue_scripts', 'custom_js');

With those addded, I created the scripts in my js folder-

/js/drag.js

(function() {
        $(".drag").draggable({
                helper: function(event) {
                        return $('<div class="drag-row"><table></table></div>').find('table').append($(event.target).closest('tr').clone()).end();
                },
        });
});

And /js/date.js

( function() {
        $( "#datepicker" ).datepicker();
});

In my php page I added the date id to a calendar, and a class id to the table row as targets for the jquery scripts.

<p>Date: <input type="text" id="datepicker"></p>

        echo "<div id='table'>";
        echo "<table class='listing'>";
                echo "<thead>";
                        echo "<tr>";
                                echo "<th><b>Course Name</b></th>";
                                echo "<th><b>Fiscal Year</b></th>";
                                echo "<th><b>Course Number</b></th>";
                                echo "<th><b>Start Date</b></th>";
                                echo "<th><b>End Date</b></th>";
                        echo "</tr>";
                echo "</thead>";
                echo "<tbody>";
                        foreach($courses as $course){
                                echo "<tr class='drag'>";
                                        echo "<td>".$course->course_name."</td>";
                                        echo "<td>".$course->fy."</td>";
                                        echo "<td>".$course->course_number."</td>";

                                        $date = $course->course_date_start;
                                        $date = strtotime($date);

                                        if ($date){
                                                echo "<td>".date("m/d/Y", $date)."</td>";
                                        } else {
                                                echo "<td></td>";
                                        }

                                        $date = $course->course_date_end;
                                        $date = strtotime($date);

                                        if ($date){
                                                echo "<td>".date("m/d/Y", $date)."</td>";
                                        } else {
                                                echo "<td></td>";                                               }
                                        echo "</tr>";
                        }
                echo "</tbody>";
        echo "</table>";

When my page loads, the scripts are all there (including jquery, I put spaces since the jquery lines are at line 350, and the jquery-ui lines are at line 500), but the ui isn't functional. Anyone have any insights?

<script type='text/javascript' src='##/wp-includes/js/jquery/jquery.js?ver=1.12.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.4.1'></script>




<script type='text/javascript' src='##/wp-content/themes/twentysixteen/js/functions.js?ver=20160816'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/core.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/widget.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/mouse.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/accordion.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/position.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/menu.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/wp-a11y.min.js?ver=4.6.1'></script>
<script type='text/javascript'>
/* <![CDATA[ */
var uiAutocompleteL10n = {"noResults":"No search results.","oneResult":"1 result found. Use up and down arrow keys to navigate.","manyResults":"%d results found. Use up and down arrow keys to navigate."};
/* ]]> */
</script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/autocomplete.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/slider.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/tabs.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/sortable.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/draggable.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/droppable.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/datepicker.min.js?ver=1.11.4'></script>
<script type='text/javascript'>
jQuery(document).ready(function(jQuery){jQuery.datepicker.setDefaults({"closeText":"Close","currentText":"Today","monthNames":["January","February","March","April","May","June","July","August","September","October","November","December"],"monthNamesShort":["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],"nextText":"Next","prevText":"Previous","dayNames":["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],"dayNamesShort":["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],"dayNamesMin":["S","M","T","W","T","F","S"],"dateFormat":"MM d, yy","firstDay":1,"isRTL":false});});
</script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/resizable.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/button.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-includes/js/jquery/ui/dialog.min.js?ver=1.11.4'></script>
<script type='text/javascript' src='##/wp-content/themes/twentysixteen/js/date.js?ver=4.6.1'></script>
<script type='text/javascript' src='##/wp-content/themes/twentysixteen/js/drag.js?ver=4.6.1'></script>
<script type='text/javascript' src='##/wp-includes/js/wp-embed.min.js?ver=4.6.1'></script>

</div>
  • 写回答

1条回答 默认 最新

  • dongran1779 2016-11-20 13:53
    关注

    You can tell the wp_enqueue_script() function that your scripts depend on jQuery, so they will be inserted in correct way and order, notice the third parameter, for example:

     wp_enqueue_script( 'jquery-ui-core', false, array('jquery'));
    

    Read more here.

    Also note that jQuery UI Effects is not included with the jquery-ui-core handle.

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮