Я использую следующий фрагмент с сайта W3school. Когда я использовал элемент LI по прямой линии, он работает нормально. Автобус, как я выразился в формате, не работает. У кого-то есть ключ?Javascript: Текстовый узел
Это не работает:
<ul id="myList">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
<p>Click the button to replace the first item in the the list.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var textnode = document.createTextNode("Water");
var item = document.getElementById("myList").childNodes[0];
item.replaceChild(textnode, item.childNodes[0]);
}
</script>
Это работает:
<ul id="myList"><li>Coffee</li><li>Tea</li><li>Milk</li></ul>
<p>Click the button to replace the first item in the the list.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var textnode = document.createTextNode("Water");
var item = document.getElementById("myList").childNodes[0];
item.replaceChild(textnode, item.childNodes[0]);
}
</script>
Что значит «не работает»? Какая ошибка дает вам? – fbid