简体   繁体   English

JavaScript显示/隐藏html元素

[英]JavaScript show / hide html elements

Is there a cleaner way of achieving this in JavaScript, I'm trying to show / hide items based on a click function 有没有更干净的方法可以在JavaScript中实现这一点,我正在尝试基于click函数显示/隐藏项目

var overview = document.getElementById("overview"); 
var schedule = document.getElementById("schedule");
var reports = document.getElementById("reports");

    function btnOverview_Click() {
        schedule.style.display = "none";
        reports.style.display = "none";
        overview.style.display = "block";
    }       
    function btnSchedule_Click() {
        overview.style.display = "none";
        reports.style.display = "none";
        schedule.style.display = "block";
    } 
    function btnReports_Click() {
        overview.style.display = "none";
        schedule.style.display = "none";
        reports.style.display = "block";
    } 

Avoiding all possible inline styling and JavaScript, 避免所有可能的内联样式和JavaScript,
I suggest you to do the following: 我建议您执行以下操作:

 // I added a class in your HTML var elements = document.querySelectorAll(".myClass"); var buttons = document.querySelectorAll(".myButtons"); // Function to hide all "elements" except the one we clicked function hideExceptThis() { elements.forEach(elm => elm.style.display = "none"); // Get the value in "data" attribute var id = this.getAttribute("data"); // "this" refers to the one you clicked document.querySelector(id).style.display = "block"; } // On load, bind the above function on click for each element buttons.forEach(but => but.addEventListener("click", hideExceptThis)); 
 #panel { border-bottom: 1px solid gray; background: #eee; padding: 8px; } .myClass { width: 240px; height: 40px; margin: 8px; padding: 8px; } #overview { background-color: #fdd; } #schedule { background-color: #dfd; } #reports { background-color: #ddf; } 
 <div id="panel"> <button class="myButtons" data="#overview">Only overview</button> <button class="myButtons" data="#schedule">Only schedule</button> <button class="myButtons" data="#reports">Only reports</button> </div> <div class="myClass" id="overview">Overview</div> <div class="myClass" id="schedule">Schedule</div> <div class="myClass" id="reports">Reports</div> 

Hope it helps. 希望能帮助到你。

This is not really clean, but at least it saves some lines of code. 这不是很干净,但是至少可以节省一些代码行。

Pass this (the element that fires the function) to your function and set all elements to display: none; this (触发函数的元素)传递给函数,并将所有元素设置为display: none; , but set the element that got clicked back to display: block; ,但将要单击的元素设置为display: block; .

 var overview = document.getElementById("overview"); var schedule = document.getElementById("schedule"); var reports = document.getElementById("reports"); function hide(element) { overview.style.display = "none"; schedule.style.display = "none"; reports.style.display = "none"; element.style.display = "block"; } 
 <div id="overview" style="width: 100px; height: 15px; background-color: green" onclick="hide(this)"></div> <div id="schedule" style="width: 100px; height: 15px; background-color: yellow" onclick="hide(this)"></div> <div id="reports" style="width: 100px; height: 15px; background-color: blue" onclick="hide(this)"></div> 

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

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