При объединении 3 текстовых узлов в 3 разных абзаца первые два ps не объединяются в одном из текстовых узлов. Он должен объединить имя Роберт между двумя другими текстовыми узлами. Он работает только в последней части цикла. (см. последнюю строку, чтобы узнать, как это должно выглядеть). На консоли не отображается ошибка.
Вы можете сказать мне, почему он не работает? Или, может быть, есть лучший способ объединить текстовые узлы, о которых вы знаете?
Это школьное упражнение; Я должен использовать DOM на JavaScript. Прилагаю скриншоты результата.
document.addEventListener('DOMContentLoaded', function() {
var sing = document.getElementById('sing');
sing.addEventListener('click', function() {
var friend1 = document.createElement('div');
friend1.className = "friend";
var friend1h3 = document.createElement('h3');
var robert = document.createTextNode('Robert');
friend1h3.appendChild(robert);
document.body.appendChild(friend1h3);
for (var i = 99; i > 0; i--) {
var friend1p = document.createElement('p');
if (i > 2) {
var a = document.createTextNode((i) + " lines of code in the file, " + (i) + " lines of code; ");
friend1p.appendChild(a);
friend1p.appendChild(robert);
var b = document.createTextNode(" strikes one out, clears it all out " + (i - 1) + " lines of code in the file");
friend1p.appendChild(b);
} else if (i === 2) {
var c = document.createTextNode("2 lines of code in the file, 2 lines of code; ");
friend1p.appendChild(c);
friend1p.appendChild(robert);
var d = document.createTextNode(" strikes one out, clears it all out 1 line of code in the file");
friend1p.appendChild(d);
} else {
var e = document.createTextNode("1 line of code in the file, 1 line of code; ");
friend1p.appendChild(e);
friend1p.appendChild(robert);
var f = document.createTextNode(" strikes one out, clears it all out, 0 lines of code in the file");
friend1p.appendChild(f);
}
document.body.appendChild(friend1p);
}
})
})<button type = "button" id = "sing">Click</button>


![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Вы перемещаете узел Роберт вместо добавления клон
If the given child is a reference to an existing node in the document, appendChild() moves it from its current position to the new position (there is no requirement to remove the node from its parent node before appending it to some other node).
document.addEventListener('DOMContentLoaded', function() {
var sing = document.getElementById('sing');
sing.addEventListener('click', function() {
var friend1 = document.createElement('div');
friend1.className = "friend";
var friend1h3 = document.createElement('h3');
var robert = document.createTextNode('Robert');
friend1h3.appendChild(robert);
document.body.appendChild(friend1h3);
for (var i = 10; i > 0; i--) {
var friend1p = document.createElement('p');
if (i > 2) {
var a = document.createTextNode((i) + " lines of code in the file, " + (i) + " lines of code; ");
friend1p.appendChild(a);
friend1p.appendChild(robert.cloneNode());
var b = document.createTextNode(" strikes one out, clears it all out " + (i - 1) + " lines of code in the file");
friend1p.appendChild(b);
} else if (i === 2) {
var c = document.createTextNode("2 lines of code in the file, 2 lines of code; ");
friend1p.appendChild(c);
friend1p.appendChild(robert.cloneNode());
var d = document.createTextNode(" strikes one out, clears it all out 1 line of code in the file");
friend1p.appendChild(d);
} else {
var e = document.createTextNode("1 line of code in the file, 1 line of code; ");
friend1p.appendChild(e);
friend1p.appendChild(robert.cloneNode());
var f = document.createTextNode(" strikes one out, clears it all out, 0 lines of code in the file");
friend1p.appendChild(f);
}
document.body.appendChild(friend1p);
}
})
})<button type = "button" id = "sing">Click</button>
Подсказка:
appendChildперемещает узел DOM; он не создает копию. Если вы остановите цикл на полпути, где вы увидите «Роберт»?