How to convert selected HTML to Json?

前端 未结 6 867
迷失自我
迷失自我 2020-12-03 14:27

I want to save part of my html code into json as a file then recap back the html codes for editing. Any idea how can i do it?

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-03 15:05

    function htmlToJson(div,obj){
     if(!obj){obj=[]}
     var tag = {}
     tag['tagName']=div.tagName
     tag['children'] = []
     for(var i = 0; i< div.children.length;i++){
        tag['children'].push(htmlToJson(div.children[i]))
     }
     for(var i = 0; i< div.attributes.length;i++){
        var attr= div.attributes[i]
        tag['@'+attr.name] = attr.value
     }
     return tag    
    }
    

提交回复
热议问题