[英]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.