<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Работа со свойствами DOM-узлов в JavaScript</title>
<script src="scripts.js"></script>
</head>
<body>
<!-- Начало body -->
<br>
Текст в теге body.<br><br>
</body>
</html>
window.onload = function()
{
var bodyInnerHtml = document.body.innerHTML;
var bodyOuterHtml = document.body.outerHTML;
bodyInnerHtml = bodyInnerHtml.replace(/\</g, '<');
bodyInnerHtml = bodyInnerHtml.replace(/\>/g, '>');
bodyInnerHtml = bodyInnerHtml.replace(/\n{2,}/g, '\n');
bodyInnerHtml = bodyInnerHtml.replace(/\s{1,}\n/g, '<br>');
bodyInnerHtml = bodyInnerHtml.replace(/^\n|\n$/g, '');
bodyInnerHtml = bodyInnerHtml.replace(/\n/g, '<br>');
bodyInnerHtml = bodyInnerHtml.replace(/\s/g, ' ');
bodyOuterHtml = bodyOuterHtml.replace(/\</g, '<');
bodyOuterHtml = bodyOuterHtml.replace(/\>/g, '>');
bodyOuterHtml = bodyOuterHtml.replace(/\n{2,}/g, '\n');
bodyOuterHtml = bodyOuterHtml.replace(/\s{1,}\n/g, '<br>');
bodyOuterHtml = bodyOuterHtml.replace(/^\n|\n$/g, '');
bodyOuterHtml = bodyOuterHtml.replace(/\n/g, '<br>');
bodyOuterHtml = bodyOuterHtml.replace(/\s/g, ' ');
var resultContainer = document.createElement('div');
document.body.append(resultContainer);
resultContainer.innerHTML += '<br><b><body> innerHTML:</b><br><br>'+bodyInnerHtml+'<br><br><b><body> outerHTML:</b><br><br>'+bodyOuterHtml+'<br><br><br><b>Тип, тег и содержимое <nobr>DOM-узлов</nobr>:</b><br>';
var nodesCollection = document.body.childNodes;
var processedNodeValue = '';
for(var i = 0; i < nodesCollection.length-1; i++)
{
switch(nodesCollection[i].nodeType)
{
case 1:
resultContainer.innerHTML += '<br>DOM-element: <'+nodesCollection[i].tagName.toLowerCase()+'>';
break;
case 3:
processedNodeValue = nodesCollection[i].nodeValue.replace(/^\s{1,}|\s{1,}$|\n/g, '');
if(processedNodeValue) resultContainer.innerHTML += '<br>'+nodesCollection[i].nodeName+': «'+processedNodeValue+'»';
break;
case 8:
resultContainer.innerHTML += '<br>'+nodesCollection[i].nodeName+': «'+nodesCollection[i].nodeValue+'»';
break;
}
}
};
Результат выполнения задания можно посмотреть
здесь.