使用node.js访问DOM [英] Access to DOM using node.js

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

问题描述

我想访问html文件,并使用node.js通过id获取元素,这是我的html文件:

i want to access to html file and get an element by id using node.js, this is my html file :

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Diagram </title>

<script>

    function generatePNG (oViewer) {
// some other code
            reader.onloadend = function() {
                base64data = reader.result;
                var image = document.createElement('img');
                image.setAttribute("id", "GraphImage");
                image.src = base64data;
                document.body.appendChild(image);
            }

        }, "image/png", oImageOptions);
        return sResult;

        var sResult = generatePNG (oEditor.viewer);

    });
</script>


</head>

<body >
    <div id="diagramContainer"></div>
</body>
</html>

我想要获取 document.getElementById(GraphImage)。src ,但与node.js.
我发现我可以使用 cheerio jsdom 来访问 DOM 与node.js,所以我试过这个代码与 cheerio

I want to do get document.getElementById("GraphImage").src but with node.js. I've found that I can use cheerio or jsdom to acces the DOM with node.js, so I've tried this code with cheerio:

var cheerio = require('cheerio'),
    $ = cheerio.load('file.html');

但是,我没有遵守允许我获取图像的指令.src 从html文件,像这样的说明: document.getElementById(GraphImage)。src

But I didn't founnd the instruction that allow me to get the image.src from the html file, like this instruction: document.getElementById("GraphImage").src

推荐答案

cheerio.load()接受一个字符串作为参数。通过设置: cheerio.load('file.html') cheerio将尝试实现 DOM 从字符串 file.html 。显然,这不是你想要的。

cheerio.load() accepts a string as the argument. By setting: cheerio.load('file.html') cheerio will try to implement DOM from the string file.html. Obviously, that is not what you want.

您应该首先从您的文件中获取 html 数据,然后将其传递给 cheerio 。另外,作为@Quentin来说,cheerio是一个削减jQuery的实现,所以你应该使用jQuery选择器来获取一个ceratin元素。对于你的具体情况,它将是: $(#GraphImage)。以下是您的代码如何:

You should get the html data from your file first, then pass it to the cheerio. Also as @Quentin metioned, cheerio is a cut down implementation of jQuery, so you should use jQuery selectors to get a ceratin element. For your particular case it would be: $("#GraphImage"). Here is how your code should look like:

 var cheerio = require('cheerio'),
     $ = cheerio.load('file.html'),
     fs = require('fs');
 fs.readFile('./index.html', function (err, html) {
    if (err) {
        throw err; 
    } else {
        $ = cheerio.load(html.toString());
        console.log($('#GraphImage').attr('src'));   
    }

编辑:

另外,在html您提供的文件,您将使用javascript的方式将一些对象附加到DOM。如果要在服务器上访问它们,则应在此处解释javascript,您可以使用类似于 phantomjs 实现它,但事情变得复杂得多。

Also, in the html file that you have provided, you are appending some objects to the DOM with the help of javascript. If you want to access them on the server, the javascript should be interpreted there. You can use something like phantomjs to achieve it, but things get much more complicated.

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

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