简体   繁体   English

如何简化这个 JavaScript 函数。 我有 100 个按钮控制 100 个内容区域的显示/隐藏显示

[英]How could this JavaScript function be simplified. I have 100 buttons controlling the show/hide display of 100 content areas

The abbreviated JS file below provides the same functionality for 100 buttons.下面的缩写 JS 文件为 100 个按钮提供了相同的功能。

All buttons are identified by ID names such as #btn1, #btn2 etc.所有按钮均由 ID 名称标识,例如 #btn1、#btn2 等。

The buttons trigger the hide/show of content contained within div tags labelled within corresponding class names such as .btn1, .btn2, etc.这些按钮会触发隐藏/显示包含在相应类名称(如 .btn1、.btn2 等)中标记的 div 标签中的内容。

For example, selecting #btn1 is tied to the content within content content content .例如,选择 #btn1 与 content content content 中的内容相关联。

The process is to select a button, then whichever button is selected, hide the content within all the 100 DIVs and then show the selected button's associated content.该过程是选择一个按钮,然后选择哪个按钮,隐藏所有 100 个 DIV 中的内容,然后显示所选按钮的关联内容。

In writing the JS file I have written out the whole function 100 times - listing each one of 100 buttons to be selected, all 100 div areas to be hidden, and then the one div area to be shown.在编写 JS 文件时,我已经写了 100 次整个函数 - 列出要选择的 100 个按钮中的每一个,要隐藏所有 100 个 div 区域,然后显示一个要显示的 div 区域。

How could this be simplified into a smarter and more concise function?如何将其简化为更智能、更简洁的功能?

// JavaScript Document
$(document).ready(function() {
  $('#btn0').click(function() {
    location.reload();
  });
});

$(document).ready(function() {
  $('#btn1').click(function() {
    $('.btn0').hide();
    $('.btn1').hide();
    $('.btn2').hide();
    $('.btn3').hide();
    $('.btn4').hide();
    $('.btn5').hide();
    $('.btn6').hide();
    $('.btn7').hide();
    $('.btn8').hide();
    $('.btn9').hide();
    $('.btn10').hide();
    $('.btn11').hide();
    $('.btn11').hide();
    $('.btn12').hide();
    $('.btn13').hide();
    $('.btn14').hide();
    $('.btn15').hide();
    $('.btn16').hide();
    $('.btn17').hide();
    $('.btn18').hide();
    $('.btn19').hide();
    $('.btn20').hide();
    $('.btn21').hide();
    $('.btn22').hide();
    $('.btn23').hide();
    $('.btn24').hide();
    $('.btn25').hide();
    $('.btn26').hide();
    $('.btn27').hide();
    $('.btn28').hide();
    $('.btn29').hide();
    $('.btn30').hide();
    $('.btn31').hide();
    $('.btn32').hide();
    $('.btn33').hide();
    $('.btn34').hide();
    $('.btn35').hide();
    $('.btn36').hide();
    $('.btn37').hide();
    $('.btn38').hide();
    $('.btn39').hide();
    $('.btn40').hide();
    $('.btn41').hide();
    $('.btn42').hide();
    $('.btn43').hide();
    $('.btn44').hide();
    $('.btn45').hide();
    $('.btn46').hide();
    $('.btn47').hide();
    $('.btn48').hide();
    $('.btn49').hide();
    $('.btn50').hide();
    $('.btn51').hide();
    $('.btn52').hide();
    $('.btn53').hide();
    $('.btn54').hide();
    $('.btn55').hide();
    $('.btn51').hide();
    $('.btn52').hide();
    $('.btn53').hide();
    $('.btn54').hide();
    $('.btn55').hide();
    $('.btn56').hide();
    $('.btn57').hide();
    $('.btn58').hide();
    $('.btn59').hide();
    $('.btn60').hide();
    $('.btn61').hide();
    $('.btn62').hide();
    $('.btn63').hide();
    $('.btn64').hide();
    $('.btn65').hide();
    $('.btn66').hide();
    $('.btn67').hide();
    $('.btn68').hide();
    $('.btn69').hide();
    $('.btn70').hide();
    $('.btn71').hide();
    $('.btn72').hide();
    $('.btn73').hide();
    $('.btn74').hide();
    $('.btn75').hide();
    $('.btn76').hide();
    $('.btn77').hide();
    $('.btn78').hide();
    $('.btn79').hide();
    $('.btn80').hide();
    $('.btn81').hide();
    $('.btn82').hide();
    $('.btn83').hide();
    $('.btn84').hide();
    $('.btn85').hide();
    $('.btn86').hide();
    $('.btn87').hide();
    $('.btn88').hide();
    $('.btn89').hide();
    $('.btn90').hide();
    $('.btn91').hide();
    $('.btn92').hide();
    $('.btn93').hide();
    $('.btn94').hide();
    $('.btn95').hide();
    $('.btn96').hide();
    $('.btn97').hide();
    $('.btn98').hide();
    $('.btn99').hide();
    $('.btn100').hide();
    $('.btn98').hide();
    $('.btn99').hide();
    $('.btn100').hide();
    $('.btn1').show();
  });
});

$(document).ready(function() {
  $('#btn2').click(function() {
    $('.btn0').hide();
    $('.btn1').hide();
    $('.btn2').hide();
    $('.btn3').hide();
    $('.btn4').hide();
    $('.btn5').hide();
    $('.btn6').hide();
    $('.btn7').hide();
    $('.btn8').hide();
    $('.btn9').hide();
    $('.btn10').hide();
    $('.btn11').hide();
     …………………….. BTN12 to 97 ……………………..
    $('.btn98').hide();
    $('.btn99').hide();
    $('.btn100').hide();
    $('.btn1').show();
  });
});

Etc., up to 100 buttons等,最多 100 个按钮

// JavaScript Document // JavaScript 文档

Assuming you can't change the html structure, I would probably do:假设您无法更改 html 结构,我可能会这样做:

$('[id^="btn"]').on('click', function() {
    const id = $(this).attr('id');
    $('[class^="btn"]').hide();
    $(`.${id}`).show();
});

Which will listen to the click event on any element where the id starts with btn , then hide all elements where the class starts with btn , then show the element with the same class as the id that was just clicked (eg #btn2 click will show .btn2 )它将侦听 id 以btn开头的任何元素上的click事件,然后隐藏类以btn开头的所有元素,然后显示与刚刚单击的 id 具有相同类的元素(例如, #btn2 click 将显示.btn2 )

something like this.像这样的东西。

    for(let i = 0;i<=99;i++){
        let btnclass= ".btn" + i;
        $(btnclass).hide()
     }

You can use a for loop to iterate from 0 to 100 :您可以使用for 循环0100进行迭代:

$(document).ready(function() {
    $("#btn1").click(function() {
        for (let i = 0; i <= 100; i++) {
            $(`.btn${i}`).hide();
        }
    });
})

Full version:完整版:

// JavaScript Document
$(document).ready(function() {
  $("#btn0").click(function() {
    location.reload();
  });
});
$(document).ready(function() {
  $("#btn1").click(function() {
    for (let i = 0; i <= 100; i++) {
      $(`.btn${i}`).hide();
    }
  });
})
$(document).ready(function() {
  $("#btn2").click(function() {
    for (let i = 0; i <= 100; i++) {
      $(`.btn${i}`).hide();
    }
  });
});

Common class and data attributes along with event delegation makes the code easier to maintain.通用类和数据属性以及事件委托使代码更易于维护。

 document.querySelector("#wrapper").addEventListener("click", function (event) { var toggles = event.target.dataset.toggles; // Hide previous selected elements var selectedElems = document.querySelectorAll(".out.selected"); if (selectedElems.length){ selectedElems.forEach(function (elem) { elem.classList.remove("selected"); }); } // show the new active elements const activeElems = document.querySelectorAll(toggles); if (activeElems.length){ activeElems.forEach(function (elem) { elem.classList.add("selected"); }); } });
 .out { display: none; } .out.selected { display: block; }
 <div id="wrapper"> <button id="btn1" data-toggles=".out1">1</button> <button id="btn2" data-toggles=".out2">2</button> <button id="btn3" data-toggles=".out3">3</button> <button id="btn4" data-toggles=".out4">4</button> </div> <div class="out out1">1</div> <div class="out out2">2</div> <div class="out out3">3</div> <div class="out out4">4</div>

If you want to use jQuery如果你想使用 jQuery

 $("#wrapper").on("click", "[data-toggles]", function (event) { var toggles = $(this).data('toggles'); $(".out.selected").removeClass("selected"); $(toggles).addClass("selected"); });
 .out { display: none; } .out.selected { display: block; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="wrapper"> <button id="btn1" data-toggles=".out1">1</button> <button id="btn2" data-toggles=".out2">2</button> <button id="btn3" data-toggles=".out3">3</button> <button id="btn4" data-toggles=".out4">4</button> </div> <div class="out out1">1</div> <div class="out out2">2</div> <div class="out out3">3</div> <div class="out out4">4</div>

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

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