【发布时间】:2020-03-05 15:04:27
【问题描述】:
我想要显示数组的项目。单击按钮时,我需要执行以下操作:
- 在显示数组的开头、结尾或中间添加一个元素
- 对数组进行排序
- 从数组中删除重复项
我设法显示数组的项目,并使用我的 Add in the Front 按钮一一添加新项目。但是字符串值不会显示,我只能看到索引号 1.、2.、3. 等。
我尝试将id="firsttree" 放在<input type="text"> html 标记中,但是当我将它加载到网页上时该数组会消失。
JS
var originalArray = ['spruce', 'pine', 'cedar', 'maple', 'oak', 'birch', 'aspen'];
// Iterate array and display
originalArray.forEach(element => {
// Creates an Element Node with the specified name.
let listItem = document.createElement("li");
// creates a Text Node with the specified text.
let itemDescription = document.createTextNode(element);
// append: Add Item or Element
listItem.appendChild(itemDescription);
/*document.getElementById("firsttree").innerHTML = originalArray;*/
// Returns the element that has the ID attribute with the specified value and adds a node to the end of the list of children of the specified parent node
document.getElementById("firsttree").appendChild(listItem);
});
/**
* Add Items to list
*/
function addItem() {
// Unshift: Add item to beginning of list
originalArray.unshift(document.getElementById('firsttree').value);
// Making the text box empty and re-display
document.getElementById('firsttree').value = " ";
disp(); // displaying the array elements
}
/**
* Render array
*/
function disp() {
var str=" ";
str = originalArray.length + '<br> '; // break the lines to form list.
// Increment the list by 1, i++ if i is less than length
for (i=1; i < originalArray.length; i++) // Initial parameter is 1.
{
// Adding each element with key number to display string
str += i + ". "+ originalArray[i] + "<br> ";
}
// Display the elements of the array
document.getElementById('firsttree').innerHTML=str;
}
HMTL
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="activity3.css">
<!-- ol: Ordered numbered list-->
<!--<script>
/*var trees = ['spruce', 'pine', 'cedar', 'maple', 'oak', 'birch', 'aspen'];*/AbortSignal
/*document.getElementById("oringinalTree").innerHTML = trees;*/
</script>-->
</head>
<body>
<h1>Array Methods</h1>
<br>
<label>
Enter new array element here
</label>
<input type="text">
<button type="button" value="Add" onclick="addItem()" >
Add in the Front
</button>
<button type="text">
Add at the End
</button>
<button type="text">
Add in the Middle
</button>
<button type="text">
Sort
</button>
<button type="text">
Remove Duplicates
</button>
<br>
</form>
<h2>List of Trees</h2>
<h3>Tree Display:</h3>
<!-- Must create div to place list under Header-->
<div class="originalArray">
<ol id="firsttree"></ol>
<!--<link rel="stylesheet" type="text/css" href="path/to/your.css" />-->
<script src="gla3.js"></script>
</div>
<h4>Sorted Tree Display:</h4>
</body>
</html>
css
h3 {
position: absolute;
left: 70px;
top: 200px;
}
h4 {
position: absolute;
left: 300px;
top: 200px;
}
.originalArray {
position: absolute;
top: 250px;
left: 50px;
}
我需要将input 中的字符串添加到数组中然后显示,但我开始时将undefined 添加到数组中,其余的都是空白的。另外,我也不希望看到增量数 (12)。
【问题讨论】:
-
<script src="gla3.js">文件中有什么内容? -
我的 Javascript 文件
标签: javascript html arrays