简体   繁体   English

怎么用javascript onClick function改logo?

[英]how to use javascript onClick function to change the logo?

hello guys hope you're doing well, im new to front-end and specifically javascript, so im working on a design to train myself, i added a button that onClick changes the menu style, and it worked but i tried changing the logo img src and it didn't work, it changes the img after two clicks and the image used before the click does not appear again when the button unclicked大家好,希望你们做得好,我是前端新手,特别是 javascript,所以我正在设计一个训练自己的设计,我添加了一个按钮,onClick 改变了菜单样式,它有效,但我尝试改变标志 img src 并且它不起作用,它在两次单击后更改了 img 并且当未单击按钮时,单击之前使用的图像不会再次出现

<script>
  let sidebar = document.querySelector(".sidebar");
  let sidebarBtn = document.querySelector(".sidebarBtn");
  sidebarBtn.onclick = function () {
    sidebar.classList.toggle("active");
    if (sidebar.classList.contains("active")) {
      sidebarBtn.classList.replace("bx-menu", "bx-menu-alt-right");
      document.getElementById("img").src = "mdn-logo.png";
    } else {
        sidebarBtn.classList.replace("bx-menu-alt-right", "bx-menu");
    }
  };
</script>

I think you're probably looking for the mouseup event, if that's what you mean by "unclick".我认为您可能正在寻找mouseup事件,如果这就是“取消单击”的意思。

https://developer.mozilla.org/en-US/docs/Web/API/Element/mouseup_event https://developer.mozilla.org/en-US/docs/Web/API/Element/mouseup_event

Also, please refrain from using Element.onclick as it's quite outdated and deprecated.此外,请不要使用Element.onclick ,因为它已经过时且已弃用。 Instead, learn to use Element.addEventListener .相反,学习使用Element.addEventListener You have more options and it's easier to manage as well.您有更多选择,并且也更易于管理。 Read more on MDN .阅读更多关于MDN的内容。

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

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