【问题标题】:Jade - script not being recognizedJade - 无法识别脚本
【发布时间】:2014-08-11 06:14:52
【问题描述】:

我有这个非常简单的jade文档,我想导入标准的jQuery脚本:

extends layout

block content
    h1 #{title}

    ul#messages
    form#formAddUser(name="adduser",method="post",action="")
        input#m(type="text", name="message")
        button#btnSubmit(type="submit") submit

    script(src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js")

这是

的扩展
doctype html
html
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')

  body
    block content

问题是脚本甚至无法识别!我宁愿有一个 404 响应,但即便如此,什么也没有发生。我做错了什么?

非常感谢

PS:我正在使用 express.js

控制器代码:

router.get("/chat",function(req,res){
    res.render("chat",{title:"Chat"});
});

更新: 我在布局中添加了一块

doctype html
html
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
    script(type="text/javascript")
      alert("hello!")
  body
    block content

那个简单的警报,它也不起作用!

【问题讨论】:

  • 您的更新完全不同。内联脚本需要在 script 之后添加一个点。
  • 您预计会发生什么?你只是包含了 jQuery,它不会仅仅通过包含它来做任何事情。
  • 感谢内联脚本的提示。对于您的第二条评论,如果我尝试将那个简单的警报放在本地脚本中并调用它,它只会发送 404
  • 我想我在这里遗漏了一些基础知识。不应该包括 jquery 成功发送 200 响应?
  • "不应该包括 jquery 成功发送 200 响应吗?"什么应该发送 200 响应到哪里?您是否看到使用 Firebug 或类似工具的 //ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js 出现 404 错误?

标签: javascript jquery node.js express pug


【解决方案1】:

为jade中的文字解析添加一个点。即。

doctype html
html
    head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
    script(type="text/javascript").
        alert("hello!")
        console.log('Notice the dot after the script tag.');
body
    block content

如果你添加一个点,那么任何 within 块(如此缩进)都不会被解析。像这样:

ul
    li This is inlined text.
    li.
       But this one is on its' own line, without the But being turned into a tag.
    li
       div this is a div inside a li
    li.
       div this is not a div, simple text inside a li.

p For added bonus, you can also use a colon and the next thing is also a tag.
p like, opposite of the dot.
p: a(href="#") Clickme!
p a(href="#") I'm not clickable :(

翡翠很酷,不是吗?

编辑:糟糕,刚刚注意到 alert('hello') 只是您的原始问题的附录。让我更新答案。

编辑 2: 实际上,您能否显示您渲染的 HTML(对于第一个示例),以便我可以看到实际渲染的内容以及您期望的内容?

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 2015-05-26
  • 2020-03-14
  • 1970-01-01
  • 2017-08-15
  • 2020-12-02
  • 1970-01-01
相关资源
最近更新 更多