javascript Vanila JS和CSS

ijxebb2r  于 2023-04-19  发布在  Java
关注(0)|答案(1)|浏览(99)

如何使按钮在屏幕上呈现时消失

function findRequireMessage() {
  const messages = document.querySelectorAll('.message');
  const requireMessage = Array.from(messages).find(message => message.className.includes('requestedReply'));
  if (requireMessage) { 
    requireMessage.scrollIntoView({ block: 'end', behavior: 'smooth' });
    return true;
  } else { 
    alert('No require message found.');
    return false;
  }
}

window.addEventListener("DOMContentLoaded", (event) => {
  const findRequireButton = document.getElementById('find-require-button');
  findRequireButton.style.display = 'none'; // set the button to display none initially
  if (findRequireMessage()) {
    findRequireButton.style.display = 'block';
  }
});

我想要这个显示:“倾斜块时,第一次加载,以检查是否有一个requireMessage在聊天,如果它是真的,以显示按钮,否则不显示任何聊天

rt4zxlrg

rt4zxlrg1#

有一些方法可以做到这一点,例如:
1.将defer添加到HTML <script>标记

<script src="path/to/js" defer></script>

1.使按钮从一开始就不可见

<button id="find-require-button" style="display:none;"></button>

1.在DOMContentLoaded之后等待一段时间(这不是最佳实践,因为用户可能会看到按钮消失)

window.addEventListener("DOMContentLoaded", (event) => {
    setTimeout(()=>{}, 50 /* 50 milliseconds after DOMContentLoaded; this can be changed to any value */)
    // code
})

相关问题