简体   繁体   English

如何确保javascript是Jade模板中代码的底部?

[英]How to ensure the javascript is at the bottom of the code in Jade template?

I'm new to Jade template. 我是Jade模板的新手。 Here is the layout.jade: 这是layout.jade:

html
  head
    meta(charset='UTF-8')
    title MyApplication
    meta(content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no', name='viewport')
    link(href='/bootstrap/dist/css//bootstrap.min.css', rel='stylesheet', type='text/css')

  body
    block content

script(src='http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js')
script(src='/bootstrap/dist/js/bootstrap.min.js', type='text/javascript')

As you see, there are two javascript at the end of the template file. 如您所见,模板文件的末尾有两个JavaScript。 My another jade file, home.jade is: 我的另一个玉文件home.jade是:

extends layout

block content

        // Content Header (Page header)
        section.content-header
          h1= title

        // Main content
        section.content

script(type='text/javascript').
  $(function() {
     alert('here!');
  }

The javascript related to this file is at the end of the file. 与该文件相关的javascript位于文件末尾。 Unfortunately the block content is embedded inside the layout.jade so that the script here is loaded before the jQuery. 不幸的是, 块内容嵌入在layout.jade内部,因此此处的脚本在jQuery之前加载。 The code will never be called. 该代码将永远不会被调用。 Is there any way to solve this problem to make sure the javascript here is loaded after the jQuery? 有什么方法可以解决此问题,以确保此处的javascript在jQuery之后加载? Thanks 谢谢

You can define more blocks in your layout to fill. 您可以在布局中定义更多块以进行填充。 You can even define default values for blocks: see http://jade-lang.com/reference/extends/ 您甚至可以为块定义默认值:请参见http://jade-lang.com/reference/extends/

layout.jade: layout.jade:

html
  head
    meta(charset='UTF-8')
    title MyApplication
    meta(content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no', name='viewport')
    link(href='/bootstrap/dist/css//bootstrap.min.css', rel='stylesheet', type='text/css')

  body
    block content

    script(src='http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js')
    script(src='/bootstrap/dist/js/bootstrap.min.js', type='text/javascript')

    block script

home.jade: home.jade:

extends layout

block content

        // Content Header (Page header)
        section.content-header
          h1= title

        // Main content
        section.content

block script
    script(type='text/javascript').
      $(function() {
        alert('here!');
      }

This will render your content, js libraries and your script respectively. 这将分别渲染您的内容,js库和脚本。

In layout.jade, indent script tags inside body. 在layout.jade中,在主体内缩进脚本标记。

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

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