相关疑难解决方法(0)

WordPress 在古腾堡块内添加 onclick 事件

我一直在到处寻找以找出如何在古腾堡块内添加事件。我正在努力使用 ACF 和 Foundation 添加手风琴系统。我使用 ACF 创建了一个块、字段和模板。我希望我的用户能够在视觉模式下打开和关闭手风琴。

我确实找到了以下脚本来监视块何时更改。唯一的问题是它在加载所有内容之前触发,我必须使用超时来允许块完全加载。我一直无法找到更好的方法来实现这一目标。有什么建议么?

const getBlockList = () => wp.data.select( 'core/editor' ).getBlocks();
let blockList = getBlockList();
wp.data.subscribe(() => {
    const newBlockList = getBlockList();
    const blockListChanged = newBlockList !== blockList;
    blockList = newBlockList;
    if ( blockListChanged ) {
        setTimeout(function(){
            jQuery(document).foundation();
            Foundation.reInit($('[data-accordion]'));
        }, 4000);
    }
});
Run Code Online (Sandbox Code Playgroud)

javascript wordpress wordpress-gutenberg gutenberg-blocks

7
推荐指数
1
解决办法
7259
查看次数