简体   繁体   English

如何撤消 JavaScript function? 第一个 function 有效,但我想用第二个 function 撤消它,但那个不起作用

[英]How to undo a JavaScript function? The first function works, but I want to undo it with the second function, but that one does not work

    <script>
      const elem= document.getElementById("close");

The following function removes the div container like I wanted:以下 function 像我想要的那样删除了div容器:

      window.onload = function(){
        document.getElementById("close").onclick = function(){
            this.parentNode.parentNode.parentNode
            .removeChild(this.parentNode.parentNode);
            return false;
        };
    
      };

The following function should undo the effect of the first function automatically when the user scrolls back to the top, but it does not work.下面的 function 应该在用户滚动回顶部时自动撤消第一个 function 的效果,但它不起作用。

        window.addEventListener('scroll', function() {
        document.getElementById('showScroll').innerHTML = window.pageYOffset;
        if (window.pageYOffset===0){
          document.getElementById("close")=elem;
          }
          return false;
        });
    </script>

The main problem is with this line:主要问题在于这一行:

document.getElementById("close")=elem;

It should be something like:它应该是这样的:

document.getElementById("container").appendChild(elem);

The problem is that the element with the id of close is no longer in the DOM (it was removed when the button was clicked) and so you cannot refer to it in the code once it has gone.问题是idclose的元素不再在 DOM 中(单击按钮时它已被删除),因此一旦它消失,您就无法在代码中引用它。

Therefore, you need to refer to its parent, for example container , and append the button elem as a child to the container .因此,您需要将其父元素(例如container )和elem按钮元素作为子元素引用到container If elem is not at the end of the container then you have to work out where to append it.如果elem不在container的末尾,那么您必须找出 append 的位置。

However, there are also multiple parentNodes in the first code, which are probably not necessary.但是,第一个代码中也有多个parentNodes ,这可能不是必需的。

Here is a detailed reference: MDN Node.appendChild()这是一个详细的参考: MDN Node.appendChild()

Here is a demonstration that shows you this working:这是一个演示,向您展示了这项工作:

 const elem = document.getElementById("close"); window.onload = function() { document.getElementById("close").onclick = function() { this.parentNode.removeChild(this); }; }; window.addEventListener('scroll', function() { document.getElementById("showScroll").textContent = 'scrollY: ' + Math.floor(window.pageYOffset); if (window.pageYOffset === 0) { document.getElementById("container").appendChild(elem); } });
 #container { border: solid 3px red; padding: 10px; } #showScroll { position: fixed; top: 0px; margin-left: 100px; background: yellow; }
 <div id="container"> <p id="showScroll">scrollY: 0</p> <p>Test</p> <p>Test</p> <p>Test</p> <p>Test</p> <p>Test</p> <p>Test</p> <p>Test</p> <p>Test</p> <p>Test</p> <p>Test</p> <p>Test</p> <p>Test</p> <p>Test</p> <p>Test</p> <p>Test</p> <p>Test</p> <p>Test</p> <p>Test</p> <p>Test</p> <p>Test</p> <p>Test</p> <p>Test</p> <button id="close"> Close </button> </div>

The fix修复

if you use this:如果你使用这个:

this.parentNode.parentNode.removeChild(this.parentNode);

then its opposite is this:那么它的反面是这样的:

this.parentNode.parentNode.appendChild(element);

So to sum it up:所以总结一下:

// this will remove the element but keep it saved inside elem
let keep = this.parentNode.parentNode.removeChild(this.parentNode);
// this will place back the element you removed
this.parentNode.parentNode.appendChild(keep);

that is because document.getElementById("close") has already been removed.那是因为document.getElementById("close")已经被删除了。

A better solution更好的解决方案

A better solution to this if you are going to insert back the element anyways is to play with its css.如果您无论如何要插入该元素,一个更好的解决方案是使用它的 css。
Just add display:none in its css and the element will be the same as gone (without actually getting deleted).只需在其 css 中添加display:none ,该元素将与消失相同(实际上并未被删除)。

// hide the element without deleting it
document.getElementById("close").style.display = "none";
// make the element visible again
document.getElementById("close").style.display = "block";

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

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