- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为我的任务制作一份装箱单,我们可以选择任何地点和任何 3 件元素。应该发生的是用户按下 1、2 或 3 按钮,然后会出现一个提示,告诉用户他们想要用什么交换所选项目。然后,一旦他们点击确定,新项目就会出现在列表中。因此,如果他们按 1 并键入一个项目,则新项目将在 1 中。如果用户想要交换 2 中的项目或 3 中的项目,也会发生同样的情况。但是,我遇到的问题是它不显示新项目。它允许我按 1、2 或 3,然后键入我想要与之交换的任何项目,然后添加到列表中,列表末尾出现 4。要交换项目,作业要求使用 replaceChild() 方法。我是不是在编码中输入了错误或忘记了什么?感谢您的帮助。
<!DOCTYPE html>
<html lang="en">
<head>
<title>CIS 223 Chapter 10 Program</title>
<meta charset="utf-8">
<script>
var list;
function createDOM()
{
list = document.getElementById("items");
var newElement = document.createElement("LI");
var nodeText = document.createTextNode("Sunscreen");
newElement.appendChild(nodeText);
list.appendChild(newElement);
var newElement = document.createElement("LI");
var nodeText = document.createTextNode("Water");
newElement.appendChild(nodeText);
list.appendChild(newElement);
var newElement = document.createElement("LI");
var nodeText = document.createTextNode("Swim suits");
newElement.appendChild(nodeText);
list.appendChild(newElement);
}
//Swap items function.
function registerKey(keyEvent)
{
var keyValue = keyEvent.key;
if (keyValue < "1" || keyValue > "3")
{
alert("Only press 1, 2, or 3");
return;
}
var userInput;
var newInput;
var newElement = document.createElement("LI");
//Prompt user for new entry.
userInput = prompt("Enter a new item for slot "+keyValue,"");
//Check input for valid entry.
if (userInput != null && userInput != "")
{
//Write Your code to Pass string input to Text Node.
// .......
newInput = document.createTextNode("");
//Write Your code to Attach Text Node to Element.
// .......
newElement.appendChild(newInput);
list.appendChild(newElement);
var whichNode = parseInt(keyValue); // which item to be replaced
//Write Your code to Replace existing node with new node.
// .......
nodeText.replaceChild(newInput,nodeText.childnodes[LI]);
}
}
</script>
</head>
<body onload=createDOM() onkeypress=registerKey(event)>
<div id="wrapper">
<header>
<h1>Summer Vacation</h1>
</header>
<main>
<h2>We're going to Cancun, Mexico!</h2>
<p>Let's pack a few <em>essentials</em> for the trip.</p>
<ol id="items">
</ol>
<p>These provisions were carefully selected for this trip.<br><br>
However, you can swap out any of the three items by pressing:<br>
<strong>1</strong>, <strong>2</strong>, or <strong>3</strong> on your keyboard.</p>
</main>
</div>
</body>
</html>
最佳答案
因为这是一个作业,我不会给你修复的代码,但这里是需要修复的主要问题:
您需要使用用户输入的内容创建文本节点。目前newInput = document.createTextNode("");
创建一个带有空字符串的文本节点 ""
.您需要将用户的输入传递给它。
nodeText
在你的 registerKey
中不存在功能。函数体内定义的变量 {}
仅存在于该函数体(范围)内。因此,无论您在何处尝试访问 nodeText
在你的 registerKey
里面函数你会得到一个错误 - 你可以阅读更多关于作用域的信息 here .
您不需要使用 list.appendChild(newElement);
.这会将您的新项目附加/添加到列表末尾的列表中。这不是您想要的行为。相反,您的脚本将使用 .replaceChild()
将该项目添加到您的列表中。 .所以你可以删除这一行。
nodeText.replaceChild(newInput,nodeText.childnodes[LI]);
也不正确。 .replaceChild()
的思路|就是传入你想要替换旧元素的新元素。用例是这样的:
parentNode.replaceChild(newChild, oldChild);
在您的例子中,新元素是 newElement
,这是 <li>
您创建的包含用户输入值的元素,而不是文本节点 newInput
.同样,这里 nodeText
不存在,所以你需要使用 list
而不是 nodeText
因为那是包含您的列表项的(父)元素/<li>
元素作为 child 。
nodeText.childnodes[LI]
也会给你带来问题。如上所述,nodeText
需要是 list
.此外,childnodes
需要改为childNodes
.这会给你一个 NodeList
您的 list
中的子项目,类似于数组。回想一下,您需要将项目作为第二个参数传递给 replaceChild()
需要是您要从列表中替换/删除的项目,因此需要使用 whichNode
从节点列表中获取此项目数字。你可以看看 NodeList.item()
帮助您实现这一目标。请注意,您已经创建了带有空格的 HTML 元素:
<ol id="items">
</ol>
意思是使用.childNodes
在你的空<ol>
将为您提供 NodeList 索引 0 处的元素。我建议您删除此空格并使用 <ol id="items"></ol>
相反:
var items = document.getElementById("items1");
console.log(items.childNodes); // empty NodeList
var items = document.getElementById("items2");
console.log(items.childNodes); // NodeList with 1 item (undesirable)
<ol id="items1"></ol>
<ol id="items2">
</ol>
关于javascript - 使用 Javascript、DOM 和 replaceChild() 方法创建装箱单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68514882/
请告诉我哪种算法适用于以下问题: 在给定的 3 个月内,我们的项目数量有限(通常 < 50)。每个项目都有一定的小时数。 我们在同一 3 个月内拥有有限数量的资源(通常 < 100)。 每个资源每个月
我了解装箱和拆箱是关于强制转换(将实型转换为对象...将对象转换为实型)的。但是我不明白MSDN对Nullable的评价。这是我不明白的文字: When a nullable type is boxe
据我了解,Java 具有装箱(堆)和未装箱(堆栈)变量,如果我将装箱类型分配给未装箱类型,反之亦然,则涉及 (n) (un) 装箱成本。 拆箱比分配一个新的装箱对象便宜吗?如果以只读方式使用,盒装对象
装箱/拆箱和类型转换之间有什么区别? 这些术语似乎经常可以互换使用。 最佳答案 装箱是指将不可空值类型转换为引用类型或将值类型转换为其实现的某个接口(interface)(例如 int 到 IComp
给定一组项目,每个项目都有一个值,确定要包含在集合中的每个项目的数量,以使总值小于或等于给定限制,并且总值尽可能大。 例子: Product A = 4Product B = 3Product C =
我正在编写一个一维装箱程序。我只想要一个可能的垃圾箱。所以它不包括很多垃圾箱,它是唯一的一个。该程序仅搜索四边形组,如果四边形组不等于搜索数量则爆炸。我想搜索大于四边形的每个可能的组。在示例中,我们有
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我最近的代码包括很多 boxing and unboxing ,因为我的许多变量都是在运行时解析的。但是我读到装箱和拆箱在计算上非常昂贵,所以我想问一下是否还有其他方法可以对类型进行装箱/拆箱?这甚至
我的作业是装箱问题,要求我展示如何从优化版本解决问题的决策版本,反之亦然。我知道要解决决策版本,您只需获取优化版本中使用的 bin 数量并将其与指定的最大 bin 数量进行比较,但我如何使用决策版本来
这是在不使用外部库的情况下在 Java 中装箱/拆箱多维原始数组的最有效方法吗? private Float[][] toFloatArray(float[][] values) { Float
我有一个头疼的问题,感觉类似于经典的装箱问题,但我无法确定。任何帮助表示赞赏... 问题: 我有一组尺寸相同的产品,但有不同颜色的款式和不同的订单数量要求。 我可以任意组合包装,但我只能有规定数量的不
我有一个由 df.column.value_counts().sort_index() 生成的 Pandas 系列。 | N Months | Count | |------|------| |
大家可以向我解释一下 new 的性质以及 Integer 的使用 Integer i = new Integer(-10); Integer j = new Integer(-10); Integer
如果我错了,请纠正我,将值类型装箱转换为引用类型,那么为什么以下代码给出 10 输出而不是 12? public static void fun(Object obj) { o
如果我理解 CLR 装箱和处理可空值的方式,如 Boxing / Unboxing Nullable Types - Why this implementation? 中所述,我仍然有一些困惑。例如,
我经常使用 div 来装箱信息,但我注意到包装箱有时可能不会填满空间,这会导致来自其他包装器的元素进入箱区域。 例如: my info1 my Info2 看看这个例子 .topic {
目前我有这门课 public class Currency { private int _Amount; public Currency(){... } public Curr
我收集了 43 到 50 个数字,范围从 0.133 到 0.005(但大部分偏小)。如果可能的话,我想找到 L 和 R 之间总和非常接近的所有组合。* 蛮力法需要 243 到 250 步,这是不可行
我试图从性能的角度了解两种解决方案中的哪一种是首选。比如我有两段代码: 1) 装箱/拆箱 int val = 5; Session["key"] = val; int val2 = (int)Sess
通过 C# 在装箱/拆箱值类型上从 CLR 中提取 ... 关于装箱:如果可为空的实例不是null,CLR 会从可为空的实例中取出值并将其装箱。换句话说,值为 5 的 Nullable 装箱到值为
我是一名优秀的程序员,十分优秀!