【问题标题】:Jade: How to include a javascript fileJade:如何包含 javascript 文件
【发布时间】:2012-12-30 04:52:45
【问题描述】:

我需要在网页中包含一个 javascript 文件。我写了以下内容:

include /../scripts/jquery.timeago.js

但我明白了

<script>/*
 * timeago: a jQuery plugin, version: 0.8.2 (2010-02-16)
 * @requires jQuery v1.2.3 or later
 *
 * Timeago is a jQuery plugin that makes it easy to support automatically
 * updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago").
 *
 * For usage and examples, visit:
 * http://timeago.yarp.com/
 *
 * Licensed under the MIT:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright (c) 2008-2010, Ryan McGeary (ryanonjavascript -[at]- mcgeary [*dot*] org)
 */
(function($) {
....
</script>

结果。但我需要:

<script src="/Scripts/jquery.timeago.js" type="text/javascript"></script>

怎么做?

【问题讨论】:

  • includes 用于将一个 pug 文件的内容包含到另一个文件中。 script 就是你要找的。​​span>

标签: express pug


【解决方案1】:

把这个放在你的玉文件里:

script(src="/Scripts/jquery.timeago.js")

【讨论】:

  • 别忘了把它放在&lt;body&gt; 标签的末尾,而不是放在&lt;head&gt; 中,这样可以加快页面加载速度
【解决方案2】:

你可以把这段代码放在你的jade文件中:

   script(type='text/javascript' src='public/vendor/jquery/jquery.min.js')

【讨论】:

    【解决方案3】:

    另外,如果你想在你的jade文件中包含内联js,你也可以这样做:

    script(type="text/javascript").
    console.log('hello world');
    

    【讨论】:

      【解决方案4】:

      将脚本添加到您的翡翠文件的语法是

      script(src="",...,otherAttribute="")
      

      像下面这样的示例,将 Jquery Bootstraps 添加到页面。

        script( src="https://code.jquery.com/jquery-3.2.1.slim.min.js",  integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" ,crossorigin="anonymous")
      
      script(src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js", integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q", crossorigin="anonymous")
      
        script( src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" ,integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl", crossorigin="anonymous") 
      

      【讨论】:

      • 仅发布代码有点粗鲁,也不是很有帮助。你介意解释一下吗?并且可以添加文档链接?
      • 很抱歉,我只是赶时间,Jade脚本中添加脚本的语法(src="",...,otherAttribute="") 并且可以添加到Html中的任何地方页面,但上面的代码只是为引导添加 Jquery 的一种快速方法,最好在结束正文标记之前添加它。
      猜你喜欢
      • 2016-04-04
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-19
      • 2013-09-04
      • 2015-11-23
      相关资源
      最近更新 更多