简体   繁体   English

如何通过单击按钮显示页面元素?

[英]How can I show page elements by clicking on button?

First I'll show my code首先,我将展示我的代码

const mainButton = document.getElementById("start__button").addEventListener("click", function(event){
    event.target.parentNode.removeChild(event.target);
});

By clicking button, I want it to disappear and then appear new elements on page like navbar etc. The problem is I can't handle it at this point and I need some help:P通过单击按钮,我希望它消失,然后在导航栏等页面上出现新元素。问题是我现在无法处理它,我需要一些帮助:P

As indicated by the tags on your posts, you are using jQuery.如您帖子上的标签所示,您使用的是 jQuery。 So, try the following:因此,请尝试以下操作:

First, add the display: none style to all elements that should be hidden at the beginning.首先,将display: none样式添加到所有应该在开始时隐藏的元素。 You can for convenience use a hidden class.为方便起见,您可以使用隐藏的 class。

.hidden {
  display: none;
}

Then, add an onclick event to the button that hides the button and reveals all previously hidden elements.然后,将 onclick 事件添加到隐藏按钮并显示所有以前隐藏的元素的按钮。

$("start__button").on("click", function() {
  $(this).hide();
  $(".hidden").show();
});

 const mainButton = document.getElementById("start__button").addEventListener("click", function(event){ document.getElementById("navbar").classList.toggle("hidden"); });
 .hidden{ display:none; }
 <navbar id="navbar">My navbar body....</navbar> <button id="start__button">My Button</button>

This might help you这可能会帮助你
By the classList.toggle() function, you can toggle the class of the navbar or any other html element on clicking the button and after that using simple css, you can not only hide or show the element but also do other changes By the classList.toggle() function, you can toggle the class of the navbar or any other html element on clicking the button and after that using simple css, you can not only hide or show the element but also do other changes
Removing the whole element from the document and then again adding it by element.innerHTML = "..." is not recommended Thanks.不建议从文档中删除整个元素,然后再通过element.innerHTML = "..."添加它 谢谢。

You can group all of the content you want to show after click in a wrapper element.您可以在单击包装元素后对要显示的所有内容进行分组。

 const mainButton = document.getElementById("start__button"); mainButton.addEventListener("click", function(event){ this.remove(); document.querySelector('main').classList.remove('hidden') });
 .hidden { display: none; } main > * { padding: 1rem; } nav, footer { background: black; color: #fff; }
 <button id="start__button">start</button> <main class="hidden"> <nav>Navigation</nav> <section>Content</section> <footer>Footer</footer> </main>

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

相关问题 如何通过单击另一个html页面上的按钮来编辑页面? - How can I edit a page by clicking a button on the another html page? 如何通过单击按钮显示隐藏文本? - How can I show a hidden text by clicking on a button? 通过单击按钮显示消息,但我如何通过单击按钮隐藏此消息 - show message by clicking button but how i can hide this message by clicking the button 如何通过单击按钮在页面中包含html文件? - How can I include an html file in my page by clicking on a button ? 如何通过单击Javascript中的按钮跳到另一页? - How can I jump to another page by clicking a button in Javascript? 如何通过单击按钮来增加或减少页面的字体大小 - How can I increase or decrease font size of page by clicking on a button 如何在单击按钮上显示标签并防止页面刷新 - How to show <tr> tag on clicking button and prevent page refresh 如何通过单击功能的“提交”按钮来传递各个动态创建的元素的ID-javascript - How can I pass id of individual dynamically created elements by clicking on submit button to a function - javascript 如何在不单击 React Native 应用程序中的按钮的情况下显示警报消息? - How can I show Alert message without clicking a button in React Native app? Javascript 通过单击按钮显示/隐藏某些元素 - Javascript Show/Hide certain elements by clicking a button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM