使type =“ module”的javascript进入队列。 [英] Enqueue javascript with type="module"
问题描述
我想在wordpress中自定义主题上使用countUp.js。
I want to use countUp.js on my custom theme in wordpress.
当我使用wp_enqueue_script()添加文件时,出现错误:
When I add the file with wp_enqueue_script(), i get an error:
Uncaught SyntaxError: Unexpected token 'export'
我已经阅读到它可以在标签 type = module 上进行固定设置,但是我不知道该怎么做,因为该选项确实可以nt exixst in wp_enqueue_script ...
I've read that it can be fixed setting on the label type="module", but I don't know how to do that, as that option does'nt exixst in wp_enqueue_script...
任何人都可以伤害我吗?
Anyone can hel me?
推荐答案
一个人可以通过应用过滤器 script_loader_tag 来向脚本添加属性。
One can add attributes to a script by applying filter 'script_loader_tag'.
使用 add_filter('script_loader_tag','add_type_attribute',10,3);
添加过滤器。
定义回调函数,如上面链接中给出的示例:
Define the callback function like the example given on the link above:
function add_type_attribute($tag, $handle, $src) {
// if not your script, do nothing and return original $tag
if ( 'your-script-handle' !== $handle ) {
return $tag;
}
// change the script tag by adding type="module" and return it.
$tag = '<script type="module" src="' . esc_url( $src ) . '"></script>';
return $tag;
}
这篇关于使type =“ module”的javascript进入队列。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!