HTML page for json object

I want to add an html page to a json object and want to access the div tags there id from the json object. I am new to this and do not know what I want to do.

the scenario is as follows:

I have an html page that has multiple div tags with id = page1, page 2, etc. I want to add this page to a json object and then access its page id in js ie page1 page2, etc. Can anyone suggest some solution for this.

I just saw some values ​​being entered in json object.how to add any html page or its elements?

+4
source share
2 answers
0

, JSON .

, innerHTML body JSON.

var body = [].slice.call(document.getElementsByTagName('body'))[0];
json.Content = body.innerHTML;

JSON , . DOM , .

var parser = new DOMParser();
var htmlDoc = parser.parseFromString(json.Content, "text/html");

:

var data = '{"id":"1", "Title":"Page", "Content":""}';
var json = JSON.parse(data);

var body = [].slice.call(document.getElementsByTagName('body'))[0];
json.Content = body.innerHTML;

var parser = new DOMParser();
var htmlDoc = parser.parseFromString(json.Content, "text/html");

console.log(htmlDoc.getElementById("Page-02").innerText);
<div id="Page-01">
  1. Lorem ipsum dolor sit amet, consectetur adipiscing elit
</div>
<div id="Page-02">
  2. sed do eiusmod tempor incididunt ut labore et dolore magna aliqua
</div>
<div id="Page-03">
  3. Ut enim ad minim veniam
</div>
0

Source: https://habr.com/ru/post/1667731/


All Articles