Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

javascript - How to compare two DOMs or DOM nodes in general?

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.

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) { ... }

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 isn't neccessary similar bodyNodes2[123].innerHTML

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. :-)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think what you are looking for is either:

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

Hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...