【发布时间】:2021-07-24 16:48:02
【问题描述】:
我正在尝试在 nodejs 中使用 jquery-ui draggable() 函数,在 JSdom 的帮助下,我能够让 jquery 与 jsdom 一起工作,但 jquery-ui 无法正常工作。 不知道如何链接两个库或使其工作
const { JSDOM } = require('jsdom');
const fs = require('fs');
const path = require('path');
const htmlPage = fs.readFileSync(path.resolve(__dirname,'../views/search.ejs'));
const { window } = new JSDOM(htmlPage);
const $ = require('jquery-ui-dist/jquery-ui')(window);
exports.getSearch = (req, res) => {
res.render('search', {pageTitle: 'Search'});
$('.item').draggable();
// console.log($('.main-head'));
}
执行npm start后出现错误
/home/jquery/node_modules/jquery-ui-dist/jquery-ui.js:14
factory( jQuery );
^
ReferenceError: jQuery is not defined
at /home/jquery/node_modules/jquery-ui-dist/jquery-ui.js:14:12
at Object.<anonymous> (/home/jquery/node_modules/jquery-ui-dist/jquery-ui.js:16:2)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/home/Nodejs/jquery/controller/search-controller.js:9:11)
[nodemon] app crashed - waiting for file changes before starting...
搜索.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="main-head">
<div class="item">
<h1>item1</h1>
</div>
<div class="item">
<h1>item2</h1>
</div>
</div>
</body>
</html>
【问题讨论】:
标签: jquery node.js express jquery-ui