简体   繁体   English

如何将 JavaScript 库导入 WordPress

[英]How to import a JavaScript Library into WordPress

I'm trying to integrate the 'Rough Notation' JS Library into my WordPress website.我正在尝试将“Rough Notation”JS 库集成到我的 WordPress 网站中。

See: https://roughnotation.com/ https://github.com/rough-stuff/rough-notation见: https ://roughnotation.com/ https://github.com/rough-stuff/rough-notation

I'm just using their basic demo code (at: https://glitch.com/~basic-rough-notation ) which uses the following script:我只是在使用他们的基本演示代码(位于: https ://glitch.com/~basic-rough-notation),它使用以下脚本:

<script type="module">
  import { annotate } from 'https://unpkg.com/rough-notation?module';
  
  const n1 = document.querySelector('em');
  const n2 = document.querySelector('strong');
  const n3 = document.querySelector('h1');
  const n4 = document.querySelector('span');
  const n5 = document.querySelector('#block');
  
  const a1 = annotate(n1, { type: 'underline', color: 'blue' });
  const a2 = annotate(n2, { type: 'circle', color: 'red', padding: 10 });
  const a3 = annotate(n3, { type: 'box', color: 'orange' });
  const a4 = annotate(n4, { type: 'highlight', color: 'yellow', iterations: 1, multiline: true });
  const a5 = annotate(n5, { type: 'bracket', color: 'red', padding: [2, 10], brackets: ['left', 'right'], strokeWidth: 3 })
  
  a1.show();
  a2.show();
  a3.show();
  a4.show();
  a5.show();
  
</script>

(I'm loading this into WordPress as 'annotate-text.js') (我将其作为 'annotate-text.js' 加载到 WordPress 中)

Initially I got the following error:最初我收到以下错误:

annotate-text.js?ver=6.0.1:1 Uncaught SyntaxError: Cannot use import statement outside a module (at annotate-text.js?ver=6.0.1:1:1) annotate-text.js?ver=6.0.1:1 Uncaught SyntaxError: Cannot use import statement outside a module (at annotate-text.js?ver=6.0.1:1:1)

but after reading through some answers on StackOverflow I set up the following script to load the RoughNotation module:但是在阅读了 StackOverflow 上的一些答案后,我设置了以下脚本来加载 RoughNotation 模块:

function annotate_scripts() 
   wp_enqueue_script( 'module_handle', get_stylesheet_directory_uri() . '/js/annotate-text.js', 
   array(), false, true );

}

add_action( 'wp_enqueue_scripts', 'annotate_scripts' );


function set_scripts_type_attribute( $tag, $handle, $src ) {
  if ( 'module_handle' === $handle ) {
    $tag = '<script type="module" src="'. $src .'"></script>';
  }
   return $tag;
}
add_filter( 'script_loader_tag', 'set_scripts_type_attribute', 10, 3 );

So, it looks like it's working and RoughNotation is loading.所以,看起来它正在工作并且 RoughNotation 正在加载。 But now I get another, different error:但现在我得到另一个不同的错误:

Uncaught TypeError: Cannot read properties of null (reading 'parentElement')
at p.attach (rough-notation.esm.js?module:1:9287)
at new p (rough-notation.esm.js?module:1:8501)
at _ (rough-notation.esm.js?module:1:12411)
at annotate-text.js?ver=6.0.1:10:18

Wondering if someone can show me how to resolve this.想知道是否有人可以告诉我如何解决这个问题。

I've checked through StackOverflow again but not sure how to implement answers to similar problems.我再次检查了 StackOverflow,但不确定如何实现类似问题的答案。

Thanks谢谢

关于如何将 Javascript 库添加到您的 Wordpress 网站有一个很棒的教程,请检查以下内容: 如何将 Javascript 库添加到 Wordpress 网站

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM