最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Display content related to only those button it is clicked in HTML - Stack Overflow

programmeradmin2浏览0评论

I have three buttons in HTML, orders and products and supplier. I want when the user clicks orders order is being shown, when the user clicks products, the product is shown, and the name of the supplier when it is clicked.

function changedata(parameter){
    if(parameter==0){
        document.getElementById('myorders').style.fontSize="25px";
    }
    else if(parameter==1){
        document.getElementById('myproducts').style.fontSize="25px";
    }
    else{
        document.getElementById('mysupplier').style.fontSize="25px";
    }
}
<button class="button" onclick="changedata(0)">ORDERS</button>
<button class="button" onclick="changedata(1)">PRODUCTS</button>
<button class="button" onclick="changedata(2)">SUPPLIER</button>
<div id="myorders">
    <p>Laptop, Earphone</p>
</div>
<div id="myproducts">
    <p>Earphone, smart watch</p>
</div>
<div id="mysupplier">
    <p>Amazon, E-kart</p>
</div>

I have three buttons in HTML, orders and products and supplier. I want when the user clicks orders order is being shown, when the user clicks products, the product is shown, and the name of the supplier when it is clicked.

function changedata(parameter){
    if(parameter==0){
        document.getElementById('myorders').style.fontSize="25px";
    }
    else if(parameter==1){
        document.getElementById('myproducts').style.fontSize="25px";
    }
    else{
        document.getElementById('mysupplier').style.fontSize="25px";
    }
}
<button class="button" onclick="changedata(0)">ORDERS</button>
<button class="button" onclick="changedata(1)">PRODUCTS</button>
<button class="button" onclick="changedata(2)">SUPPLIER</button>
<div id="myorders">
    <p>Laptop, Earphone</p>
</div>
<div id="myproducts">
    <p>Earphone, smart watch</p>
</div>
<div id="mysupplier">
    <p>Amazon, E-kart</p>
</div>

But it won't hide data and serve my need, I'm a beginner in web development, looking for kind help to show data only when the corresponding button is pressed.

Share Improve this question edited Jul 4, 2022 at 15:08 MORÈ 2,5803 gold badges18 silver badges24 bronze badges asked Jul 4, 2022 at 15:06 def __init__def __init__ 1,0661 gold badge8 silver badges22 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Try giving each element a default value of display:none in your css, as such -

#myorders,
#mysuppliers,
#myproducts {
  font-size: 25px;
  display: none;
}

This will select each element and hide them right away.

Then, when a button is pressed, you can use

document.getElementById('___').style.display = 'block';

to then show that element.

Here is the final product:

function changedata(parameter){
    if(parameter==0){
        document.getElementById('myorders').style.display = 'block';
    }
    else if(parameter==1){
        document.getElementById('myproducts').style.display = 'block';
    }
    else{
        document.getElementById('mysupplier').style.display = 'block';
    }
}
#myorders,
#myproducts,
#mysupplier{
font-size: 25px;
display: none;
}
<button class="button" onclick="changedata(0)">ORDERS</button>
<button class="button" onclick="changedata(1)">PRODUCTS</button>
<button class="button" onclick="changedata(2)">SUPPLIER</button>
<div id="myorders">
    <p>Laptop, Earphone</p>
</div>
<div id="myproducts">
    <p>Earphone, smart watch</p>
</div>
<div id="mysupplier">
    <p>Amazon, E-kart</p>
</div>

If you would like to have the element toggle between hidden and shown on each button press, I remend toggling a class with javascript, as such:

function changedata(parameter){
    if(parameter==0){
      document.getElementById('myorders').classList.toggle('active');
    }
    else if(parameter==1){
    document.getElementById('myproducts').classList.toggle('active');
    }
    else{
    document.getElementById('mysupplier').classList.toggle('active');
    }
}
#myorders,
#myproducts,
#mysupplier{
font-size: 25px;
display: none;
}
#myorders.active,
#myproducts.active,
#mysupplier.active{
display: block;
}
<button class="button" onclick="changedata(0)">ORDERS</button>
<button class="button" onclick="changedata(1)">PRODUCTS</button>
<button class="button" onclick="changedata(2)">SUPPLIER</button>
<div id="myorders">
    <p>Laptop, Earphone</p>
</div>
<div id="myproducts">
    <p>Earphone, smart watch</p>
</div>
<div id="mysupplier">
    <p>Amazon, E-kart</p>
</div>

There are slightly easier ways to connect each div to its corresponding button, and one of them is to use data attributes. We can add a data attribute to each button the text of which matches the id of its corresponding div.

(I'm assuming that when you click on one button all the other divs are hidden, and only its div shows.)

This example uses more modern JS techniques but I'll guide you through them, ment everything, and provide documentation at the end. You don't have to understand everything here but you're probably going to bump up against these things eventually, so you might as well take a look at them now.

Here's a rundown of how this all works:

  1. Remove the inline listeners from the buttons. Modern JS uses addEventListener.
  2. Wrap the buttons in a container. What we're going to use is a technique called event delegation. Instead of attaching listeners to every button we attach one to the container and this captures any events that "bubble up" the DOM from its child elements. We can then call a function when a child element is clicked.
  3. The function does a few things. First it checks to see if the clicked element was actually a button. Then it hides all the "panels" by removing a class called "show" from them ("show" sets the element's display to block - initially all panels have their display set to none). Then based on the id from the button's data attribute it forms a selector with it, and we use that to target its corresponding div and apply the "show" class.

// Cache out buttons container, and all of the panels
const buttons = document.querySelector('.buttons');
const panels = document.querySelectorAll('.panel');

// Add an event listener to the buttons container
buttons.addEventListener('click', handleClick);

// When a child element of `buttons` is clicked
function handleClick(e) {
 
  // Check to see if its a button
  if (e.target.matches('button')) {

    // For every element in the `panels` node list use `classList`
    // to remove the show class
    panels.forEach(panel => panel.classList.remove('show'));

    // "Destructure" the `id` from the button's data set
    const { id } = e.target.dataset;

    // Create a selector that will match the corresponding
    // panel with that id. We're using a template string to
    // help form the selector. Basically it says find me an element
    // with a "panel" class which also has an id that matches the id of
    // the button's data attribute which we just retrieved.
    const selector = `.panel[id="${id}"]`;

    // Select the `div` and, using classList, again add the
    // show class
    document.querySelector(selector).classList.add('show');
  }
}
.panel { display: none; }
.show { display: block; }
.button { text-transform: uppercase; }
.button:hover { cursor: pointer; background-color: #fffff0; }
<div class="buttons">
  <button data-id="myorders" class="button">Orders</button>
  <button data-id="myproducts" class="button">Products</button>
  <button data-id="mysupplier" class="button">Supplier</button>
</div>

<div class="panel" id="myorders"><p>Laptop, Earphone</p></div>
<div class="panel" id="myproducts"><p>Earphone, smart watch</p></div>
<div class="panel" id="mysupplier"><p>Amazon, E-kart</p></div>

Additional documentation

  • addEventListener

  • classList

  • Destructuring assignment

  • forEach

  • matches

  • querySelector

  • querySelectorAll

  • Template string

发布评论

评论列表(0)

  1. 暂无评论