wordpress - 在 WordPress 管理页面中包含 jQuery UI Sortable

标签 wordpress jquery-ui

我正在开发一个插件,但在尝试使用 jQuery UI Sortable 时遇到了问题。我按照 Codex 中的说明进行操作但问题仍然存在。 jQuery UI sortable 不起作用,Firebug 说 TypeError: jQuery(...).sortable is not a function .

我在 WordPress 3.6 上运行,代码是:

<?php
/*
Plugin Name: Name
Description: Description
Version: 0.1
Author: Bloorchi
*/

add_action( 'admin_menu', 'my_plugin_admin_menu' );

function my_plugin_admin_menu() {
    add_action('admin_print_scripts-' . $page_hook_suffix, 'my_plugin_admin_scripts');
    $page_hook_suffix = add_submenu_page( 'edit.php', 'My Plugin', 'My Plugin', 'manage_options', 'my_plugin-options', 'my_plugin_manage_menu' );
}

function my_plugin_admin_scripts() {
    wp_enqueue_script( 'jquery-ui-sortable' );
}

function my_plugin_manage_menu() {
?>
<table id="test">
    <tbody>
        <tr>
            <td>1</td>
            <td>2</td>
        </tr>
        <tr>
            <td>3</td>
            <td>4</td>
        </tr>
        <tr>
            <td>5</td>
            <td>6</td>
        </tr>  
    <tbody>    
</table>
<script>
    jQuery('table#test tbody').sortable();
</script>
<?php
}

最佳答案

两件事,你把它颠倒了:

$suffix = add_submenu_page( 
    'edit.php', 
    'My Plugin', 
    'My Plugin', 
    'manage_options', 
    'my_plugin-options', 
    'my_plugin_manage_menu' 
);
add_action( "admin_print_scripts-$suffix", 'my_plugin_admin_scripts');

并且您需要始终像这样运行 jQuery:
<script type="text/javascript">
jQuery(document).ready( function($) {
    $('table#test tbody').sortable();
});
</script>

关于wordpress - 在 WordPress 管理页面中包含 jQuery UI Sortable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19508860/

相关文章:

mysql - WordPress - MySQL 自动增加用户 ID

php - 为什么我的样式表没有应用我新编码的样式,即使它已正确包含在 wordpress 中

jquery - 如果在此 div 之外且在其他可拖动项内部,则可拖动恢复(同时使用无效和有效恢复选项)

jquery - 使用 jquery modal box 和 ajax 显示单元格表中的值

jquery - 在jquery中隐藏段落元素

jquery - FullCalendar "view"可缓存

wordpress - 向 WooCommerce 产品变体添加高级自定义字段

wordpress - 在 Bootstrap3 中,Modal Popup 不会出现在叠加层的顶部。如何解决这个问题

javascript - jQuery Load 不是加载 jQuery 函数

php - 从 PHP 类中删除_action