有没有可以通过DOM结构比较HTML文档的工具? [英] Are there any tools that can compare HTML documents by DOM structure?

查看:85
本文介绍了有没有可以通过DOM结构比较HTML文档的工具?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较两个HTML文档,并想知道它们是否相同。但只能通过DOM结构进行比较,这意味着忽略标记中属性的顺序,例如,< table id =onename =table> < table name =tableid =one> 是相同的。

I want compare two HTML documents, and want to know if they are the same. But only compare by DOM structure, which means ignoring the order of the attributes in a tag, for example, <table id="one" name="table">, <table name="table" id="one"> are the same.

推荐答案

我有这个问题并且能够通过使用jQuery的 .html()函数来解决它,将我的html代码放入 div 然后再将其取回,从而获得代码的规范表示。似乎在Firefox 4和IE8中工作得很好。

I had this issue and was able to solve it by using jQuery's .html() function to put my html code into a div and then take it back out again, thus getting a canonical representation of the code. Seems to work just fine in Firefox 4 and IE8 at least.

function compareHtml(a, b) {
    var div = $(document.createElement('div'));
    div.html(a);
    var aNormalized = div.html()
    div.html(b);
    var bNormalized = div.html()
    return aNormalized == bNormalized;
}

这篇关于有没有可以通过DOM结构比较HTML文档的工具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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