我最近开始学习快递。我尝试在使用res.sendFile()
express方法发送给客户端的HTML文档中使用jQuery。 HTML在浏览器上看起来不错,但控制台会返回下一条消息:
未捕获的ReferenceError:$未定义。
如何使用jQuery?
app.js
var express = require ('express');
var app = express();
app.set ('views', __dirname + '/views');
app.set ('view engine', 'jade');
app.use ( express.static(__dirname + '/static') );
app.get ('/hello', function (req, res){
res.send('Hello World');
});
app.get ('/', function (req, res){
res.sendFile(__dirname + '/views/index.html');
console.log ('IP: ' + req.ip);
});
app.get ('/headers', function(req,res){
res.set('Content-Type', 'text/plain');
var s = '';
for (var name in req.headers) s += name + ': ' + req.headers[name] + '\n';
res.send(s)
});
var server = app.listen(8080, function() {
console.log('Listening on port %d', server.address().port);
});
的index.html
<html>
<body>
<h1>Express</h1>
<script>
$("h1").value('Jesus');
</script>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
</body>
</html>
答案 0 :(得分:0)
添加到页面顶部
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$("h1").text('Jesus');
</script>
</head>
或更改您的代码
<head>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$("h1").text('Jesus');
</script>
</head>