在Zombie.js中使用jQuery [英] Using jQuery in Zombie.js

查看:122
本文介绍了在Zombie.js中使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到还有其他库旨在与Zombie一起使用类似jQuery的语法,但是出于我的目的,我特别希望/需要使用jQuery本身,以保持与某些较早编写的代码的兼容性.

I realize there are other libraries designed for the use of jQuery-like syntax with Zombie, but for my purposes, I specifically want/need to use jQuery itself in order to maintain compatibility with some earlier-written code.

我安装了npm jQuery软件包,并且一直试图将jQuery导入我的脚本中,然后使用Zombie返回的窗口进行初始化,如下所示:

I installed the npm jQuery package, and I've been trying to import jQuery into my script and then initialize it with the window returned by Zombie like so:

var Zombie= require("zombie"),
$ = require("jquery")
, zombie = new Zombie();

zombie.visit("http://www.mysite.com", function (err, browser, status) {
  if(err) {
    console.log("Error: " + err);
  }

  //Initialize a new jQuery instance with the current window returned by Zombie
  var $window = $(browser.window);
  //Get an actual jQuery object so we can search for stuff
  var document = $window("body");

  var inputBoxUsername = document.find("input#username");

  console.log(inputBoxUsername.html());
});

这是行不通的,因为它说没有html()函数,而jQuery实际上似乎没有返回我期望的对象.

This isn't working, as it says that there's no html() function, and jQuery doesn't actually seem to be returning the object I'm expecting.

有没有办法将jQuery与Zombie一起使用?

Is there a way to use jQuery with Zombie?

推荐答案

如果您的网站已经在加载jquery,则可以立即使用已加载的jQuery,在某些情况下,这会容易得多.

If your site is already loading jquery, you can use the loaded jQuery straight away, which in some cases is far easier.

var document = browser.window.$("body");

由于它是一个jQuery对象,将按预期工作.

Will work as expected, since it is a jQuery object.

我认为您仍然可以添加脚本文件,因此可以按照我刚才提到的方式添加jQuery并使用它.

I think that you can add script files anyway so you could dinamically add jQuery and use it as I just mentioned.

希望这会有所帮助!

这篇关于在Zombie.js中使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆