Adding warning when button is disabled

前端 未结 1 507
南旧
南旧 2021-01-27 06:58

I have this code:



        
相关标签:
1条回答
  • 2021-01-27 07:25

    You've got plenty of code here. It's always better to provide a minimal solution. I tried to build a minimal solution for you.

    I's CSS only, the Javascript is only for clarification on how it works.

    var buttons = document.querySelectorAll(".btn");
    
    document.getElementById("toggle-disabled").addEventListener("click", function(e){
      for(let i = 0; i < buttons.length; i++){
        buttons[i].toggleAttribute("disabled");
      }
    });
    .btn-disabled-warning {
      display: none;
    }
    
    .btn[disabled] + .btn-disabled-warning {
      color: red;
      display: block;
    }
    <div>
      <button class="btn" disabled>My Button</button>
      <p class="btn-disabled-warning">The button is disabled :-(</p>
    <div>
      
    <div>
      <button class="btn">My Button</button>
      <p class="btn-disabled-warning">The button is disabled :-(</p>
    <div>
      
    <button id="toggle-disabled">Toggle disabled</button>

    0 讨论(0)
提交回复
热议问题