1

我正在将 markItUp 用于 WP 小部件中的文本区域(即在 widgets.php 页面上,同时创建和编辑小部件)。

当我第一次打开小部件时,textarea 被标记了,但是在我单击保存后,功能就丢失了,我又回到了常规的 textarea。

我比较了页面的保存前和保存后版本的源代码,没有区别 - 很明显,因为页面没有重新加载。是否需要为每个 ajax 调用调用 jQuery?

我尝试添加

jQuery(".markitup").markItUp(mySettings);

在小部件的表单处理功能中,但这没有帮助。我也尝试将此事件绑定到保存按钮进行更改,但这似乎并没有什么不同(我很有可能完全错了)。

4

2 回答 2

3

jQuery

因此,您需要做的第一件事是挂钩 AJAX 调用,以便在保存小部件时收到通知。为此,我们将使用 jQueryajaxSuccess函数。把它放在它自己的js文件中:

// Use a self executing function so we can safely use
// $ inside and know it = jQuery
(function($){

    // Tie into all jQuery AJAX requests
    $(document).ajaxSuccess(function(e, x, o){

        // Make sure our widget is the one being saved
        // id_base will equal whatever is set in the PHP for the widget
        // In this example, we target the text widget 
        if(o.data && o.data.indexOf('id_base=text') > -1){

           // Now, lets quickly find all the right elements
           // and filter out the ones already set up, and finally
           // apply the `markItUp` call, but we will delay just to give
           // WP a chance to update the widget
           window.setTimeout( function(){
               $("textareas.markItUp:not(.markItUpEditor)").markItUp(mySettings);
           }, 200 );
        }
    });

})(jQuery);

PHP/WordPress

最后,告诉 WP在小部件页面上包含您的新 js 文件。您需要将其合并到functions.php或者如果您正在构建小部件,则将其合并到小部件 PHP 文件中:

function register_markitup(){
    wp_enqueue_script( 'markitup-widgets', WP_PLUGIN_URL . '/your-plugin/js/markitup-ajax.js' );
}

add_action( "admin_print_scripts-widgets.php", 'register_markitup' );

编辑当我发布时,我有一个不正确的add_action钩子。它需要.php我刚刚添加的。现在代码是正确的。

于 2010-01-03T04:53:43.710 回答
1

道格的解决方案效果很好。我只需要更改 window.setTimeout 函数,如下所示:

 window.setTimeout( function(){
   $("textarea.markItUp").each(function () {
      if (!($(this).hasClass('markItUpEditor'))) {
          $(this).markItUp(mySettings);
      }
    });                                    
 }, 200 );
于 2010-01-05T00:10:39.793 回答