"use strict";
var myButton = document.getElementById("myButton");
myButton.onclick = function (){
var newNode = document.createTextNode("Blueberry");
var item = document.getElementId("myList").childNodes[0];
console.log(item);
item.replaceChild(newNode,item.childNodes[0]);
}
#myButton {
color: blue;
border: 1px solid black;
width: 200px;
text-align: center;
}
<h1>Learning the Basics of How DOM Functions</h1>
<h3>
What is DOM?
</h3>
<p id="intro">
DOM stands for "Document Object Model" and it is a plaform (as well as a language-netural interface) that allows programs and scripts to dynamically access and update the content, structure, and style of a document.
<br>
<br>
Visit <a href="https://www.w3schools.com/js/js_htmldom.asp">this page </a> if you want a further explanation.
</p>
<h3>
Test Your Knowledge:
</h3>
<p>
The purpose of this homework exercise is to introduce you to some examples of DOM manipulation within the HTML. Let's get started!
</p>
<h4>
Part 1
</h4>
<p>
Write some lines of code so that when you click on the button below, one of the values in the list gets replaced without another value.
<br>
<button type="button" id="myButton">Click This to Replace</button>
<ul id="myList">
<li>
Apple
</li>
<li>
Watermelon
</li>
<li>
Pumpkin
</li>
<li>
Strawberry
</li>
<li>
Kiwi
</li>
</ul>
<b>Replace the item that doesn't belong with something that does.</b>
</p>
我创建了一个函数来创建元素createTextNode()
的文本节点onClick
,但是,它没有按预期工作。<button>
我的目标: - 我想在单击时使用该函数onClick
替换我创建的列表中的一个项目。createTextNode
<button>
这是JS Fiddle。
提前非常感谢!