将参数从Javascript传递到jquery
我希望能够将字符串传递给scrapeSite函数,以便将其添加到passedtext变量中。 当我试图将参数传递给函数时,我得到: TypeError:“undefined”不是计算“this.evaluate”的函数将参数从Javascript传递到jquery,javascript,jquery,casperjs,Javascript,Jquery,Casperjs,我希望能够将字符串传递给scrapeSite函数,以便将其添加到passedtext变量中。 当我试图将参数传递给函数时,我得到: TypeError:“undefined”不是计算“this.evaluate”的函数 而是返回一个函数。还要检查函数运行的上下文。我假设.then函数将使用定义了this.evaluate的已知上下文调用回调函数。否则,您可能需要查看.call、.apply或.bind函数 function scrapeSite(passedtext) { return f
而是返回一个函数。还要检查函数运行的上下文。我假设.then函数将使用定义了this.evaluate的已知上下文调用回调函数。否则,您可能需要查看.call、.apply或.bind函数
function scrapeSite(passedtext) {
return function() {
result = result + this.evaluate(function(){
var text = "";
$('.bottom_input_area tbody tr').each(function(){
text = text + $(this).find('td:nth-child(1)').text().trim() + ';;;;....';
text = text + $(this).find('td:nth-child(2) a').text().trim() + ';;;;....';
text = text + $(this).find('td:nth-child(3)').text().trim().replace(' :-','') + ';;;;....';
text = text + passedtext + ';;;;....';
text = text + '!!!!::::';
});
return text;
});
};
}
你有两个问题
呼叫站点
在scrapeSite内部,您可以使用它来引用casper实例。如果您这样做,这表明您将其称为以下步骤的内部:
scrapeSite.call(casper, passedtext);
评估是沙盒
是沙盒。所有内容都必须显式传递:
function scrapeSite(passedtext) {
result = result + this.evaluate(function(passedtext){
...
}, passedtext);
}
错误可能与$有关,可能是jQuery未包含在加载的页面中或不可访问,但由于我从未使用casperjs,我实际上不知道那里的作用域是如何工作的。与错误消息无关,但另一个问题是:传递给求值的回调是异步执行的,因此不能使用return。它消除了错误,但仍然无法获取文本。我做了一些输出,注意到在这一行之后:result=result+this.evaluatefunction{passedtext=undefined,但通过了上面的内容。
function scrapeSite(passedtext) {
result = result + this.evaluate(function(passedtext){
...
}, passedtext);
}