JavaScript html文档转译为对象

163 阅读1分钟

前言

由于工作中遇到一个需要将html文档转为对象的需求,在网上找到的资源都不大理想... 所以干脆自己写一下吧。

使用中如遇到bug,欢迎提issues。

安装

npm install html-to-objects --save

使用

const htmlToObjects = require('html-to-objects')
const htmlDocument = `<style>
    .app{
        font-size: 12px;
        color: #fff;
    }
</style>
<div class="app">
    <a href="#">hello world</a>
</div>
`
const result = htmlToObjects(htmlDocument)

运行结果

1639064460(1).png