问题描述
我使用jsdom与jquery,它的工作正常。然而,我试图模块化我的代码,所以我不重复自己,所以我做了一个基本的功能,从一些jsdom代码,接收一些html(DOM),调整它与jquery,并吐出来。但是,我无法返回我的结果,因此将其分配给一个调用var。我可能不会在正确的地方返回,但如果是这样,我只是没有看到明显的。可以使用一点帮助。这里是代码:
函数tweakIt(html_in){
var jsdom = require('jsdom');
jsdom.env({
html:html_in,
scripts:[
'../public/javascripts/jquery-1.7.1.min.js',
],
done:function(errors,window){
var $ = window。$;
//做一些jquery魔术和操纵dom
$('body' ).append('< div> foo< / div>');
console.log('新近操作的HTML:'+ $('body')。html()); //它完美地记录
return $('body')。html(); //这不是返回到我调用tweakIt()从哪里,为什么不?
}
});
}
var oldhtml ='< html>< body>< div>一些文本< / div>< / body>< / html>';
var newhtml = tweakIt(oldhtml); //从来没有得到设置,因为没有返回,为什么?
编辑:
异步问题,所以这里是如何使用回调而不是返回:
function tweakIt(html_in,callback){
var jsdom = require('jsdom');
jsdom.env({
html:html_in,
scripts:[
'../public/javascripts/jquery-1.7.1.min.js',
],
done:function(errors,window){
var $ = window。$;
//做一些jquery魔术和操纵dom
$('body' ).append('< div> foo< / div>');
console.log('新近操作的HTML:'+ $('body')。html()); //它完美地记录
回调($('body')。html()); //而不是返回,将结果传递给回调
}
});
}
var oldhtml ='< html>< body>< div>一些文本< / div>< / body>< / html>';
var newhtml = tweakIt(oldhtml,function(newstuff){
console.log(newstuff); // woohoo!it works!
});
我不认为你可以使用返回值,因为done:是一个异步函数。
尝试添加一个回调到你的tweakIt并通过发送它作为一个参数获得新的HTML。
tweakIt(oldHtml,function(newHtml){/ *在这里使用结果* /})
I'm using jsdom with jquery, and it's working just fine. However, I'm trying to modularize my code a bit so I don't repeat myself, so I made a basic function out of some jsdom code that takes in some html (DOM), tweaks it with jquery, and spits it back out. However, I'm unable to return my result and thus assign it to a calling var. I'm probably not returning in the right place, but I'm just not seeing the obvious if so. Could use a little help.
Here's the code:
function tweakIt(html_in){
var jsdom = require('jsdom');
jsdom.env({
html: html_in,
scripts: [
'../public/javascripts/jquery-1.7.1.min.js',
],
done: function(errors, window) {
var $ = window.$;
// do some jquery magic and manipulate the dom
$('body').append('<div>foo</div>');
console.log('Freshly Manipulated HTML: '+ $('body').html()); // it logs perfectly
return $('body').html(); // this isn't returned to where I called tweakIt() from, why not?
}
});
}
var oldhtml = '<html><body><div>some text</div></body></html>';
var newhtml = tweakIt(oldhtml); // never gets set because nothing gets returned, why?
EDIT:
It was indeed an async issue, so here's how it should be done using a callback instead of a return:
function tweakIt(html_in, callback){
var jsdom = require('jsdom');
jsdom.env({
html: html_in,
scripts: [
'../public/javascripts/jquery-1.7.1.min.js',
],
done: function(errors, window) {
var $ = window.$;
// do some jquery magic and manipulate the dom
$('body').append('<div>foo</div>');
console.log('Freshly Manipulated HTML: '+ $('body').html()); // it logs perfectly
callback($('body').html()); // instead of a return, pass the results to the callback
}
});
}
var oldhtml = '<html><body><div>some text</div></body></html>';
var newhtml = tweakIt(oldhtml, function(newstuff){
console.log(newstuff); // woohoo! it works!
});
I don't think you can do this using a return value, because done: is an async function.Try adding a callback to your tweakIt and get the new html by sending it as a parameter, e.g.
tweakIt(oldHtml, function(newHtml) {/*use the result here*/})
这篇关于如何从简单的jsdom函数返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!