Wordpress Gutenberg ACF 块打开块时如何添加 JS

Jon*_*Jon 7 javascript wordpress wordpress-gutenberg gutenberg-blocks

我正在使用 ACF 块并有以下块。

acf_register_block_type(array(
    'name'              => 'columns',
    'title'             => __('Columns'),
    'description'       => __('For complex multi colomn rows.'),
    // 'category'          => 'formatting',
    'render_template'   => get_template_directory() . '/includes/blocks/templates/columns.php',
    'enqueue_style'     => get_template_directory_uri() . '/includes/blocks/css/columns.css',
    'enqueue_script'    => get_template_directory_uri() . '/includes/blocks/js/columns.js',
    'keywords' => array('rows', 'content', 'column'),
    'supports' => array('align' => array('wide', 'full')),
    'mode' => 'auto',
));
Run Code Online (Sandbox Code Playgroud)

当我单击编辑器中的块以打开它进行编辑时,我需要运行一些 JS。我不知道是否有执行此操作的标准方法,所以我想我可以使用单击事件来运行我的函数,但它不会触发。这是 DOM 中块的图片。

阻止 HTML

我按照此处的文档添加了脚本。(底部有一个“添加块脚本”示例)

这是我精简后的 JS...

(function($){
    var initializeBlock = function ($block) {

        $('body').on('click', 'div[data-type="acf/columns"]', function () {
            console.log('teeeeeeeest');
        });

        //... Other JS put here works
    }

    if (window.acf) {
        window.acf.addAction('render_block_preview/type=columns', initializeBlock);
    }

})(jQuery);
Run Code Online (Sandbox Code Playgroud)

为什么这个点击功能不会触发?还有另一种方法可以做到这一点吗?