0

我正在尝试使用node.ioonnode.js来解析我在变量中作为字符串的 HTML 页面。

我在将 HTML 字符串node.io作为参数传递给我的工作时遇到了麻烦。

这是我节点文件中代码的摘录nodeiotest.js

var nodeIOJob   =    require('./nodeiojobfile.js');
var nodeio = require('node.io');

vat htmlString = 'HTML String Here';

nodeio.start(nodeIOJob.job, function(err, output) {
        console.log(output);
}, true);

接下来是我文件的摘录nodeiojobfile.js

var nodeio = require('node.io');

var methods = {
   input: ['xxxxxxxxxxxxxxxx'],    // htmlString is suppossed to come here
   run: function (num) {
       console.log(num);
       this.emit('Hello World!');
   }
}

exports.job = new nodeio.Job(methods);

如何htmlString在另一个文件中将我的 as 参数发送给我的工作?

此外,在收到文件后,我需要将其解析为 HTML 并执行一些基本的 CSS 选择(例如 getElementById() 等),并且需要计算offsetHeight某些 HTML 元素。文档说我可以使用get()getHTML()方法来解析 URL 的 html,但是字符串中的 HTML 呢?我该如何解析它们?

出于测试目的,我使用他以下 HTML:

<div>
    <p id="p1">
        Testing document
    </p>
</div>

我正在尝试选择<p>然后找出它的高度。

谁能帮我?提前谢谢!!

4

1 回答 1

0

I'm not familiar with node.io, but I think you want something like this:

// nodeiotest.js
...
var htmlString = 'HTML String Here';
nodeio.start(nodeIOJob.job(htmlString), function(err, output) {
  console.log(output);
}, true);

// nodeiojobfile.js
var nodeio = require('node.io');

module.exports.job = function(htmlString) { 
  var methods = {
    input: [ htmlString ],
    run  : function (num) { 
      console.log(num);
      this.emit('Hello World!');
    }
  };
  return new nodeio.Job(methods);
};
于 2013-11-21T12:51:27.727 回答