好吧,我已经开始构建自己的模块以在 casperjs 中使用,我已经到了这样的地步:我想将变量从一个函数传递到另一个函数,这就是我下面的内容
我的模块——功能
exports.accdata = function(accnum, amnum) {
var accountnumber = casper.fetchText('div.arabic:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(' + accnum + ') > td:nth-child(2) > a:nth-child(1)');
var amountwithtype =casper.fetchText('div.arabic:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(' + amnum + ') > td:nth-child(4) > div:nth-child(1)');
var redir = accountnumber.substr(1);
var split = amountwithtype.split (' ');
var amount = split[0];
var type = split[1];
};
exports.job = function (in1, in2){
console.log(in1);
console.log(in2);
};
然后我试图传递的是我的 casperjs 脚本
casper.then(function(){
universe.accdata("3", "3");
universe.job(amount, type);
});
我是个新手,确实需要帮助来了解如何设置函数来传递数据
最佳答案
您的 accdata 函数只是设置一堆变量,然后不使用它们。最简单的解决方案是将两个函数放入一个函数中,但另一种解决方案可能是从第一个函数返回一个对象并将其作为参数传递给第二个函数。
exports.accdata = function(accnum, amnum) {
return {
accountnumber: casper.fetchText('div.arabic:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(' + accnum + ') > td:nth-child(2) > a:nth-child(1)'),
amountWithType: casper.fetchText('div.arabic:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(' + amnum + ') > td:nth-child(4) > div:nth-child(1)'),
...
};
};
exports.job = function(data) {
console.log(data);
};
然后像这样使用它:
casper.then(function() {
universe.job(universe.accdata("3", "3"));
});
关于javascript - casper js 如何将变量从一个自制函数传递到另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22621720/