我在 WP 小部件中使用 markItUp 作为文本区域(即在 widgets.php 页面上,创建和编辑小部件时)。
当我第一次打开小部件时,文本区域被标记,但在单击“保存”后,功能丢失,我回到常规文本区域。
我比较了页面保存前和保存后版本的源代码,显然没有任何区别,因为页面没有重新加载。每次ajax调用都需要调用jQuery吗?
我尝试添加
jQuery(".markitup").markItUp(mySettings);
在小部件的表单处理函数内,但这没有帮助。 我也尝试进行更改,将此事件绑定(bind)到保存按钮,但这似乎没有什么区别(很有可能我全错了)。
最佳答案
jQuery
因此,您需要做的第一件事是 Hook AJAX 调用,以便在保存小部件时收到通知。为此,我们将使用 jQuery ajaxSuccess
函数。将其放入自己的 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
Hook 。它需要我刚刚添加的 .php
。现在代码是正确的。
关于jquery - markItUp for Wordpress 小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1990335/