【发布时间】:2013-10-17 07:20:38
【问题描述】:
我整天都在四处寻找,无法弄清楚为什么我的 $(document).ready 包装器中的代码都不起作用。
layout.jade
!!!5
html
head
title #{title}
link(rel='stylesheet', href='/stylesheets/style.css')
script(src='http://code.jquery.com/jquery-1.js')
script.
$(document).ready(function(){
alert("working");
$("#message").html("message set through jquery");
});
body
header
h1 Sample message here
.container
.main-content
block content
.sidebar
block sidebar
footer
block foot
着陆.翡翠
extends layout
block content
p#message Landing page
#info Info area
block foot
a(href="https://localhost:8888/logout", title="logout") Logout
控制器:
exports.landing = function(req, res) {
res.render('landing', {title: 'My Title'});
}
渲染的html:
<!DOCTYPE html><html><head><title>Dashboard</title><link rel="stylesheet" href="/stylesheets/style.css"><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script><script>$(document).ready(function(){
$("#message").html("message set through jquery");
});
alert("working");</script></head><body><header><h1>Sample Web App</h1></header><div class="container"><div class="main-content"><p id="message">Landing page</p><div id="info">Info area</div></div><div class="sidebar"></div></div><footer><a href="https://localhost:8888/logout" title="logout">Logout</a></footer></body></html>
控制台错误: 我刚刚检查了页面上的控制台,而不是我运行 Express Web 服务器的地方,发现了一个错误:
Uncaught reference error: $ is not defined
https 服务器问题:
主要问题有两个方面:1) 我使用了 @Joe 识别的错误 url。 2)由于我的网络服务器是在 Express 中创建为 https,而不是 http,因此它拒绝使用带有 @Joe 答案中列出的非安全 http 地址的 url。相反,我需要按照@Frédéric Hamidi 的建议将其修改为https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js,以便我的https Web 服务器可以使用它。
【问题讨论】:
-
渲染出来的 HTML 是什么样子的?
-
"code.jquery.com/jquery-1.js" 正在抛出 404 - 你可能打算使用这个:"//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ?
-
能否请我们看一下 html 输出的粘贴箱?控制台在说什么?谢谢
-
您的控制台有错误吗?
-
@gjw80,再想一想,尝试使用
https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js作为 jQuery 脚本的源 URL。当通过 HTTPS 提供主文档时,您的浏览器可能被配置为静默拒绝加载 HTTP 资源。
标签: javascript jquery express pug