jsdom有哪些用例

编程代码JS

1个回答

写回答

M0213

2025-07-10 01:30

+ 关注

JS
JS

JSdom是一个用于模拟浏览器环境的库,它能够在Node.JS环境中解析和操作HTML文档。它提供了许多有用的功能和用例,用于测试、爬虫和数据提取等场景。下面我们将介绍一些常见的JSdom用例,并提供相应的案例代码。

用例一:解析HTML文档

使用JSdom可以将HTML文档解析为DOM对象,从而方便地对其进行操作和提取数据。以下是一个简单的例子:

Javascript

const { JSDOM } = require("JSdom");

const html = <code>

<html>

<body>

<h1>Hello, JSdom!</h1>

Welcome to JSdom tutorial.

</body>

</html>

</code>;

const dom = new JSDOM(html);

console.log(dom.window.document.querySelector("h1").textContent);

console.log(dom.window.document.querySelector("p").textContent);

输出结果为:

Hello, JSdom!

Welcome to JSdom tutorial.

用例二:操作DOM元素

使用JSdom可以方便地对DOM元素进行增删改操作。以下是一个简单的例子:

Javascript

const { JSDOM } = require("JSdom");

const html = <code>

<html>

<body>

<div id="contAIner">

<h1>Hello, JSdom!</h1>

<img src="https://img.izhida.com/topic/d52387880e1ea22817a72d3759213819.jpg" alt="Java"><br>Java

Welcome to JSdom tutorial.

</div>

</body>

</html>

</code>;

const dom = new JSDOM(html);

const contAIner = dom.window.document.getElementById("contAIner");

const newElement = dom.window.document.createElement("p");

newElement.textContent = "This is a new paragraph.";

contAIner.appendChild(newElement);

console.log(contAIner.innerHTML);

输出结果为:

<h1>Hello, JSdom!</h1>

Welcome to JSdom tutorial.

This is a new paragraph.

用例三:模拟用户交互

使用JSdom可以模拟用户在网页上的交互操作,例如点击按钮、填写表单等。以下是一个简单的例子:

Javascript

const { JSDOM } = require("JSdom");

const html = <code>

<html>

<body>

<button id="myButton">Click me</button>

<script>

document.getElementById("myButton").addEventListener("click", function() {

document.getElementById("message").textContent = "Button clicked.";

});

</script>

</body>

</html>

</code>;

const dom = new JSDOM(html);

const button = dom.window.document.getElementById("myButton");

button.click();

console.log(dom.window.document.getElementById("message").textContent);

输出结果为:

Button clicked.

用例四:处理Ajax请求

使用JSdom可以模拟处理Ajax请求,例如获取远程数据并在网页上展示。以下是一个简单的例子:

Javascript

const { JSDOM } = require("JSdom");

const fetch = require("node-fetch");

const html = <code>

<html>

<body>

<div id="contAIner"></div>

<script>

fetch("https://api.example.com/data")

.then(response => response.JSon())

.then(data => {

document.getElementById("contAIner").textContent = data.message;

});

</script>

</body>

</html>

</code>;

const dom = new JSDOM(html, { runScripts: "dangerously" });

dom.window.fetch = fetch;

dom.window.Response = fetch.Response;

dom.window.Headers = fetch.Headers;

dom.window.Request = fetch.Request;

console.log(dom.window.document.getElementById("contAIner").textContent);

输出结果为:

Hello, JSdom!

用例五:测试网页功能

使用JSdom可以方便地进行网页功能的单元测试。以下是一个简单的例子:

Javascript

const { JSDOM } = require("JSdom");

const assert = require("assert");

function add(a, b) {

return a + b;

}

const html = <code>

<html>

<body>

<script>

const result = add(2, 3);

document.getElementById("result").textContent = result;

</script>

</body>

</html>

</code>;

const dom = new JSDOM(html, { runScripts: "dangerously" });

dom.window.add = add;

const result = dom.window.document.getElementById("result").textContent;

assert.strictEqual(result, "5");

以上是几个常见的JSdom用例,希望能对你理解JSdom的用法和功能有所帮助。你可以根据自己的实际需求灵活运用JSdom,实现更多有趣和实用的功能。

举报有用(4)分享收藏

Copyright © 2025 IZhiDa.com All Rights Reserved.

知答 版权所有 粤ICP备2023042255号