javascript - 在 expressJs 中堆栈 jquery
问题描述
我正在寻找一种从 express app.js 文件中堆叠 jquery 的方法。
也许这是不可能的,但我需要在app.js
其中使用 jquery 来修改 html 文件。
例子:
应用程序.js
const express = require('express')
const app = express()
const $ = global.jQuery = require( 'jquery' );
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname+'/index.html'));
$("#click").click(function(e){
console.log('click');
})
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
索引.html
<!doctype html>
<html>
<head>
<title>Express - Jquery</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<button id="click" type="button">Click</button>
</body>
</html>
也许我使用了错误的方法......
如果有人已经这样做了
解决方案
此代码属于客户端,而不是服务器端:
$("#click").click(function(e){
console.log('click');
})
你会把它放在你的index.html
. (或在您的 . 引用的外部脚本中index.html
。)例如:
<!doctype html>
<html>
<head>
<title>Express - Jquery</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<button id="click" type="button">Click</button>
</body>
<script type="text/javascript">
$("#click").click(function(e){
console.log('click');
});
</script>
</html>
服务器端代码没有浏览器内 DOM 的概念。它只是将页面作为对收到的 HTTP 请求的响应返回给客户端。该页面上的客户端 JavaScript 与浏览器中的 DOM 交互。
推荐阅读
- reactjs - 使用 Redux 的 Material UI AutoSuggest
- c# - ChromeDriver 超时 - 我该如何解决?
- dialogflow-es - 检测序数复数
- django - 带有 vuejs 和 django restframework 和 restauth 的 django-webpack-loader 的身份验证问题
- python - 从网页上的不同链接获取信息,并使用 Python 中的 pandas、bs4 将它们写入 .xls 文件
- c++11 - 如何初始化不可移动和不可复制的类成员 - boost::optional
- python - iPhone 上的 Django YouTube API 上传失败
- css - 如何在网页上显示带框的数字字符
- api - 仅搜索 Vimeo 评论
- null - MapStruct 为对象的所有属性映射默认值的最简单方法