简体   繁体   English

要添加document.body.appendChild,但要删除parentNode.removeChild?

[英]document.body.appendChild to add, but parentNode.removeChild to remove?

I've decided it's time for me to learn javascript in a more formal and less haphazard way than I've used it in the past. 我认为现在是时候以一种比过去使用的方式更正式,更轻松的方式学习javascript了。

So I went to developer.mozilla.org to take their tutorial. 所以我去了developer.mozilla.org来学习他们的教程。

One of the first teaching exercises they provide is in the article entitled,"A First splash into JavaScript" at https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/A_first_splash , in which they create a simple number guessing game. 他们提供的第一个教学练习之一是https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/A_first_splash上​​的标题为“ JavaScript的第一飞溅”的文章,一个简单的数字猜谜游戏。

In that game's code, the exercise creates a function called setGameOver which creates a button labeled, "Start New Game". 在该游戏的代码中,练习创建了一个名为setGameOver的函数,该函数创建了一个标记为“开始新游戏”的按钮。

The code for that button is: 该按钮的代码是:

document.body.appendChild(resetButton);

This is followed by another function called resetGame, in which this button is removed from the page: 接下来是另一个名为resetGame的函数,该函数从页面中删除:

resetButton.parentNode.removeChild(resetButton);

My question is: if the code to add the button is document.body.appendChild, why isn't the code to remove the button something like document.body.removeChild? 我的问题是:如果添加按钮的代码是document.body.appendChild,为什么不删除按钮的代码像document.body.removeChild?

And why do we have to use parentNode.removeChild instead? 为什么我们必须改用parentNode.removeChild?

This is the kind of thing with coding tutorials that drives me nuts. 编码教程让我发疯了。 They introduce a new command and they don't explain why. 他们引入了一个新命令,并且没有解释原因。 They just say in effect, "This is how you remove the button" and they leave it at that. 他们只是说:“这就是您删除按钮的方式”,然后他们就这样保留了。 Ugh! 啊!

There's a lot of ways to remove and add an element to the page, this just happens to be the way that the tutorial author chose to demonstrate that. 有很多方法可以在页面上删除和添加元素,这恰好是本教程作者选择演示的方式。 Perhaps their intention was to demonstrate that you can in fact access document.body through resetButton.parentNode , we will never know. 也许他们的意图是证明您实际上可以通过resetButton.parentNode访问document.body ,我们永远不会知道。

The main point here is that you can remove the button by accessing it's parent node. 这里的重点是您可以通过访问按钮的父节点来删除该按钮。 In this case, we know for a fact that document.body is its parent node (we added the button to that element, so it must be the parent), so in this case resetButton.parentNode and document.body refer to the same object, and are functionally equivalent. 在这种情况下,我们知道一个事实,即document.body是其父节点(我们将按钮添加到了该元素,因此它必须是父元素),因此在这种情况下, resetButton.parentNodedocument.body引用同一对象,并且在功能上等效。

var node1 = document.body;
node1.appendChild(resetButton);
// resetButton is now a child of node1 (and document.body, its the same object)
var node2 = resetButton.parentNode;
// we now know that node2 == node1 so we could do either of the following:
node1.removeChild(resetButton);
// -- or --
node2.removeChild(resetButton);

If you have the reference to a specific html node (in this case, resetButton ) and don't know what the parent node is, then using .parentNode to find the parent would be the right way to do it. 如果您引用了特定的html节点(在本例中为resetButton ), 但不知道父节点是什么,那么使用.parentNode查找父节点将是正确的方法。 If you do know the parent (because you just added it) then it doesn't really matter which way you do it. 如果您确实知道父母(因为您刚刚添加了父母),那么您以何种方式去做并不重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM