【问题标题】:Casperjs: can't find variable '$' while jQuery is injectedCasperjs:注入jQuery时找不到变量'$'
【发布时间】:2014-03-12 08:04:20
【问题描述】:

我用

注入 jquery
casper.options.clientScripts = ["jquery-latest.min.js"];

但后来我明白了

[debug] [phantom] Automatically injected jquery-latest.min.js client side
[debug] [phantom] Successfully injected Casper client-side utilities

ReferenceError: Can't find variable: $

上线

var $j_object = $(".market_listing");

已针对 casperjs 测试回答了此问题,但我没有找到将这些解决方案应用于我的问题的方法。

【问题讨论】:

    标签: jquery casperjs


    【解决方案1】:

    尝试改成这一行:

    casper.options.clientScripts.push("jquery-latest.min.js");
    

    另一种方式是这样的:

    var casper = require('casper').create({
        clientScripts: [
          'includes/jquery-latest.min.js',// These two scripts will be injected in remote
          'includes/underscore.js'   // DOM on every request
        ],
       ...
    });
    

    See the documentation about require('casper').create().

    【讨论】:

    • 我一开始是用第二种方法的,结果也没用。
    【解决方案2】:

    除了如前所述加载脚本之外,您还需要在准备好使用 jQuery 时调用评估函数。

    casper.evaluate(function(){ 
      /// Page including jQuery is loaded
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-02
      • 1970-01-01
      • 2012-03-18
      • 2014-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多