HTML2DOM

Here's this HTML-2-DOM service - http://www.html2dom.com What it does is pretty simple - you paste some HTML code and the output is JS script code that uses DOM functions to produce the same result. Could be useful when you're working on an AJAX-style app that generates new content using JavaScript.

这是HTML-2-DOM服务-http: //www.html2dom.com的作用非常简单-粘贴一些HTML代码,然后输出是使用DOM函数产生相同结果的JS脚本代码。 当您使用AJAX风格的应用程序(使用JavaScript生成新内容)时可能很有用。

I build this simple script, inspired by this great book I was reading - "Build Your Own AJAX Web Applications". In the book, the author sometimes starts with writing up what is the HTML code for the result you want to achieve, and then goes ahead with giving the DOM code. Because, you know, DOM code can be quite verbose and sometimes a bit hard to follow. So I thought, why not write up a simple tool to automate this HTML to DOM transition.

我构建了一个简单的脚本,其灵感来自于我正在阅读的一本好书-“构建自己的AJAX Web应用程序”。 在这本书中,作者有时会先写出要实现的结果HTML代码,然后再给出DOM代码。 因为,您知道,DOM代码可能很冗长,有时很难遵循。 因此,我想,为什么不编写一个简单的工具来自动执行此HTML到DOM的转换。

The code is not complicated at all, it just takes the HTML, treats it as an XML document, then loops through all the elements of the XML doc and all the attributes for each element. The script is here, hopefully reusable and you can grab it for your own projects if you wish. You can check the source of html2dom.com's index page for an example how to use the html-2-dom class.

代码一点也不复杂,它只接受HTML,将其视为XML文档,然后遍历XML文档的所有元素以及每个元素的所有属性。 该脚本在此处,希望可以重用,并且您可以根据需要将其用于自己的项目。 您可以检查html2dom.com的索引页的来源,以获取有关如何使用html-2-dom类的示例。

Some limitations of the script (that I know of) are result of the fact that I'm treating the HTML as XML document. So you might get some errors if the HTML you paste is not well-formed, with all closed tags and so on. Also you cannot use   and other entities, because XML doesn't know about them. What XML knows is only the pre-defined 5. And last, out of the different node types, my script understands only three - element, attribute and comment. I think it's enough for the practical purposes I was aiming at, even the comment type is a bit of a stretch.

脚本的一些局限性(我知道)是因为我将HTML视为XML文档。 因此,如果粘贴HTML格式不正确,带有所有关闭的标记,等等,您可能会遇到一些错误。 您也不能使用  和其他实体,因为XML不了解它们。 XML知道的只是预定义5 。 最后,在不同的节点类型中,我的脚本仅能理解三个元素,元素和注释。 我认为这足以满足我的实际目的,即使注释类型也有点麻烦。

So enjoy and as always, any feedback is welcome!

因此,一如既往地享受,欢迎任何反馈!

Tell your friends about this post on Facebook and Twitter

FacebookTwitter上告诉您的朋友有关此帖子的信息

翻译自: https://www.phpied.com/html2dom/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值