如何在 wp_footer 动作中传递变量?

我正在创建 Elementor 页面构建器小部件。我不感兴趣添加内联 java 脚本。我正在使用“ wp_footer ”操作。我想在里面传递变量add_action/wp_footer/function() { $$$ here $$$$ }.


没有 Java 脚本错误。


$var_1 = '100';

$var_2 = '200';


add_action( 'wp_footer', function() { 

<script>

   (function($) {

      'use strict';

          $('.id').numerator({

                easing: 'linear',

                duration: '<?= $var_1; ?>', 

                toValue:'<?= $var_2; ?>',

        });

    })(jQuery);

</script>

}


qq_花开花谢_0
浏览 139回答 2
2回答

温温酱

这不是一个好的做法,但如果这是你想要的,代码如下。关键是将所有内容作为字符串处理。如果您使用引号,您可以轻松地连接变量。$var_1 = '100';$var_2 = '200';add_action( 'wp_footer', function() {&nbsp;echo "<script>&nbsp;(function($) {&nbsp; 'use strict';&nbsp; &nbsp; &nbsp;$('.id').numerator({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; easing: 'linear',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; duration: '$var_1',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toValue: '$var_2',&nbsp; &nbsp; });&nbsp; })(jQuery);</script>";}

慕盖茨4494581

那些变量不应该在函数内部吗?使用全局变量怎么样?
打开App,查看更多内容
随时随地看视频慕课网APP