Node.js + jsdom,jQuery 奇怪的行为
下面的代码只是我的 server.js 文件中的一小段代码,用于运行 jsdom 文档提供的测试。
var window = jsdom.jsdom().createWindow();
jsdom.jQueryify(window, './jq.min.js' , function() {
console.log('inside');
window.$('body').append('<div class="testing">Hello World, It works</div>');
console.log(window.$('.testing').text());
console.log('end');
});
我从字面上得到的输出只是内部
,然后服务器挂起并且永远不会返回。我添加了一条调试语句 console.log(window);
来查看窗口对象是否真正被创建,并且我最终得到了一个相当大的输出语句,详细说明了该对象的内容。然而,我确实注意到的一件事是,输出不显示 $
是 window
对象的定义方法,事实上,>console.log(window.$);
呈现 undefined
。
我知道 jsdom 仍处于开发模式,但是我在这里缺少什么吗?
正如一些背景知识一样,我尝试了代码的几种变体,包括使用 jsdom.env() 方法以及从现有 HTML 标记构建文档,这两种方法都没有呈现预期结果.
The code below is just a small snippet from my server.js file just to run the test provided by the jsdom documentation.
var window = jsdom.jsdom().createWindow();
jsdom.jQueryify(window, './jq.min.js' , function() {
console.log('inside');
window.$('body').append('<div class="testing">Hello World, It works</div>');
console.log(window.$('.testing').text());
console.log('end');
});
The output I get literally is just inside
and then the server hangs and never returns. I've added a debug statement console.log(window);
to see if the window object is truly being created, and I do end up with a fairly large output statement detailing the object's contents. One thing I did notice however is that the output does not show that $
is a defined method of the window
object and in fact, console.log(window.$);
renders undefined
.
I understand jsdom is still in dev mode, but is there something I'm missing here?
Just as some background, I have tried several variations of the code, including using the jsdom.env()
method and also building a document from existing HTML markup, neither of which rendered expected results either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我希望此代码片段对您有所帮助:
从这里
I hope this code snippet helps you:
from here