Drupal 8 怎样给页面插入JS和CSS

By admin, 6 二月, 2015

drupal_add_js和drupal_add_css已经不可用了。可以通过下面方法添加JS和CSS:

假设自定义的模块名称是foobar,在模块的目录下创建文件foobar.libraries.yml(模块的目录应该是modules/foobar),内容如下:

 

foobar:

  version: 1.x

    css:

      theme: css/foo.css: {}

    js: js/bar.js: {}

    dependencies: - core/jquery

然后在模块的目录下创建js/bar.js和css/foo.css就可以了。jquery不再是Drupal 8模块加载的JS,所以有需要就加上dependencies。

在return form的时候声明加载的库:

'#attached' => [ 'library' => [ 'your_module/library_name', ], ],

参考:https://www.drupal.org/node/2274843 

评论

Restricted HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id> <img src>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。
验证码
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
请输入"Drupal10"