JQuery和Jade,文档就绪不被调用
我有一个非常基本的快速设置,以玉为视图引擎。 我的视图的标准模板存储在layout.jade中
doctype html html head title= title link(rel='stylesheet', href='/stylesheets/style.css') body block content script(src='https://code.jquery.com/jquery-2.1.3.min.js') script(src='/javascripts/global.js') script(src="/javascripts/local.js")
然后是一个index.jade文件
extends layout block content h1 Welcome script(src="/javascripts/local.js")
这个想法是,global.js包含每个视图都需要的全局设置。 Local.js特定于index.jade。
这两个脚本使用jQuery挂钩到document.ready并像这样问候用户:
$(document).ready(function(){ alert("hello from global.js"); // or local.js //... });
如果两个脚本都包含在layout.jade文件中,则会显示两个警报,但local.js文件只能在本地文件中运行。 如果local.js只包含在index.jade文件中,而不包含在两者中,则其问候语根本不显示。
显然document.ready不会从第二个jade文件中激发。 我无法弄清楚为什么。 你可能会注册多个callback: jQuery – 有多个$(document).ready(function(){})是不好的。
在这两种情况下,脚本都出现在站点源html中。
这里是nodejs控制台日志:
GET /index 304 12.661 ms - - GET /stylesheets/style.css 304 0.427 ms -- GET /javascripts/local.js 304 0.509 ms - - GET /javascripts/global.js 304 0.294 ms - -
为什么document.ready文件没有包含在第二个jade文件中?
更新:
问题是第二个脚本被放置在html文档的主体中。 显然,这会弄乱事件路由。
现在layout.jade看起来像这样:
doctype html html head title= title link(rel='stylesheet', href='/stylesheets/style.css') script(src='https://code.jquery.com/jquery-2.1.3.min.js') script(src='/javascripts/global.js') block scripts body block content
和index.jade是这样的:
extends layout block scripts script(src="/javascripts/local.js") block content h1 Welcome