gpt4 book ai didi

javascript - 如果我想要的话,如何删除我输入的最后一个元素以及所有元素?

转载 作者:行者123 更新时间:2023-12-03 01:09:54 25 4
gpt4 key购买 nike

            // Initializing the Stack Class

function Stack() {
this.dataStore = [];
this.top = 0;
this.push = push; // Inserting the element in Stack
this.pop = pop; //Removing the element in Stack
this.peek = peek;
this.clear = clear;
this.length = length;
}

// Adding an element in Stack
function push(element) {
this.dataStore[this.top++] = element;
}

function peek() {
return this.dataStore[this.top - 1];

}

// Removing an element from the given stack
function pop() {
return this.dataStore[-this.top];
}

function clear() {
this.top = 0;
}

function length() {
return this.top;
}

var s = new Stack();


function pushContainer(el) {
//console.log(el);
var x = document.getElementById("container");
x.appendChild(el);
}

function pushToStack(el) {
//
var newElement = document.createElement("p");
var Textnode = document.createTextNode(el);
newElement.appendChild(Textnode);
pushContainer(newElement);
console.log(s.top)
s.push(el);
}

function popFromStack(){
s.pop();
console.log(s.length())
}

function clearStack(){
s.clear();
let parent = document.getElementById("container");
let child = document.getElementsByTagName("p");
parent.removeChild(child)

}
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="stack.css">
</head>

<body>
<!-- <div>
<input type="text" id="stackName">
<button onclick="MakeStack()">Make a stack</button>
</div> -->
<div>
<input type="text" id="elemet">
<button onclick="pushToStack(document.getElementById('elemet').value)">Push an elemet</button>
<button onclick="popFromStack()">Pop</button>
<button onclick="clearStack()">Clear the Stack</button>
</div>
<div id="container">

</div>
<script src="script.js"></script>
</body>

</html>

我想要的是删除所有 child 。我想从 id="container"中删除所有 child 。我尝试了中音但不知何故它不起作用。popFromStac() 的逻辑也不起作用,但我的主要需求是clearStack()。 popStack() 将删除最新插入的元素,clearStack() 将删除所有元素。

最佳答案

要删除 DOM 节点内的所有元素,您有多种选择:

function removeChildrenInnerHTML() {
console.time('removing child nodes using innerHTML');
node.innerHTML = '';
console.timeEnd('removing child nodes using innerHTML');
}

function removeChildrenInLoop() {
console.time('removing child nodes using loop');
while (node.firstChild) {
node.removeChild(node.firstChild);
}
console.timeEnd('removing child nodes using loop');
}

btn1.addEventListener('click', removeChildrenInnerHTML);
btn2.addEventListener('click', removeChildrenInLoop);
<button id="btn1" type="button">Click to remove all child nodes using innerHTML = ''</button>
<button id="btn2" type="button">Click to remove all child nodes using fastest method</button>

<ul id="node">
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
</ul>

关于javascript - 如果我想要的话,如何删除我输入的最后一个元素以及所有元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52243970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com