一般如何比较两个 DOM 或 DOM 节点?

2022-01-25 00:00:00 compare nodes dom javascript

我正在使用 var win1 = open.window(...);var win2 = open.window(...); 在 Firefox 中打开 2 个选项卡/窗口 - 现在我想比较两个 DOM(文档对象模型)的相似性.所以我有两个 DOM 包含一个非常相似的页面.这些页面具有相同的 HTML,但执行不同的 JavaScript 文件.

I am using var win1 = open.window(...); var win2 = open.window(...); to open 2 tabs/windows in Firefox - now I want to compare the two DOMs (document object models) for similarity. So I have got two DOMs containing a very similar page. The pages have the same HTML but executed different JavaScript files.

一般我会检查 HTML 和 CSS 是否相同:

In general I check if HTML and CSS is the same:

    var html1 = win1.document.body.innerHTML;
var html2 = win2.document.body.innerHTML;
if (html1 == html2) { ... }
var css1 = win1.document.body.style.cssText
var css2 = win2.document.body.style.cssText
if (css1 == css2) { ... }

但是比较所有 DOM 节点似乎会给出不好的结果:

But comparing all DOM nodes seems to give bad results:

var bodyNodes1 = win1.document.body.getElementsByTagName('*');
var bodyNodes2 = win2.document.body.getElementsByTagName('*');

bodyNodes1[123].innerHTML 不需要类似 bodyNodes2[123].innerHTML

可以使用哪些方法来比较 DOM 节点?是否存在用于测试页面相似性的框架/库/脚本?

Which methods can be used to compare DOM nodes? Do any Framework/Libraries/Scripts exist for testing pages for similarity?

我非常感谢任何提示.:-)

I am very thankful for any hints. :-)

推荐答案

我认为您正在寻找的是:

I think what you are looking for is either:

isEqualNode : http://help.dottoro.com/ljlpvjmd.php
isSameNode : http://help.dottoro.com/ljqqqfft.php

isEqualNode : http://help.dottoro.com/ljlpvjmd.php
isSameNode : http://help.dottoro.com/ljqqqfft.php

希望这会有所帮助.

相关文章