代码之家  ›  专栏  ›  技术社区  ›  Az Emna

一键式拨动开关

  •  0
  • Az Emna  · 技术社区  · 7 年前

    我正在使用W3Schools提供的切换开关(链接: toggleswitch W3Schools )

    我想在选中切换开关时执行一个函数。

    问题是,当我使用click时,函数执行了两次。 它会显示两次警报(您可以试试snippet btw)

    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    .switch {
      position: relative;
      display: inline-block;
      width: 60px;
      height: 34px;
    }
    
    .switch input { 
      opacity: 0;
      width: 0;
      height: 0;
    }
    
    .slider {
      position: absolute;
      cursor: pointer;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: #ccc;
      -webkit-transition: .4s;
      transition: .4s;
    }
    
    .slider:before {
      position: absolute;
      content: "";
      height: 26px;
      width: 26px;
      left: 4px;
      bottom: 4px;
      background-color: white;
      -webkit-transition: .4s;
      transition: .4s;
    }
    
    input:checked + .slider {
      background-color: #2196F3;
    }
    
    input:focus + .slider {
      box-shadow: 0 0 1px #2196F3;
    }
    
    input:checked + .slider:before {
      -webkit-transform: translateX(26px);
      -ms-transform: translateX(26px);
      transform: translateX(26px);
    }
    
    /* Rounded sliders */
    .slider.round {
      border-radius: 34px;
    }
    
    .slider.round:before {
      border-radius: 50%;
    }
    </style>
    </head>
    <body>
    
    <h2>Toggle Switch</h2>
    
    <label class="switch" onclick="function1()">
      <input type="checkbox">
      <span class="slider"></span>
    </label>
    
    
    
    </body>
    
    
    
    
    </html> 
    <script>
    function function1(){
    alert("heelloo");
    
    }
    
    </script>

    如何正确调用函数?

    5 回复  |  直到 7 年前
        1
  •  1
  •   Bhushan Kawadkar    7 年前

    由于气泡效应,当您单击单击“移动到其父标签”的输入时,会看到两次。但是,可以使用 jQuery

    不要单击标签,而是在输入时使用on change event。见下面的代码

    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    .switch {
      position: relative;
      display: inline-block;
      width: 60px;
      height: 34px;
    }
    
    .switch input { 
      opacity: 0;
      width: 0;
      height: 0;
    }
    
    .slider {
      position: absolute;
      cursor: pointer;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: #ccc;
      -webkit-transition: .4s;
      transition: .4s;
    }
    
    .slider:before {
      position: absolute;
      content: "";
      height: 26px;
      width: 26px;
      left: 4px;
      bottom: 4px;
      background-color: white;
      -webkit-transition: .4s;
      transition: .4s;
    }
    
    input:checked + .slider {
      background-color: #2196F3;
    }
    
    input:focus + .slider {
      box-shadow: 0 0 1px #2196F3;
    }
    
    input:checked + .slider:before {
      -webkit-transform: translateX(26px);
      -ms-transform: translateX(26px);
      transform: translateX(26px);
    }
    
    /* Rounded sliders */
    .slider.round {
      border-radius: 34px;
    }
    
    .slider.round:before {
      border-radius: 50%;
    }
    </style>
    </head>
    <body>
    
    <h2>Toggle Switch</h2>
    
    <label class="switch">
      <input type="checkbox" onchange="function1()">
      <span class="slider"></span>
    </label>
    
    
    
    </body>
    
    
    
    
    </html> 
    <script>
    function function1(){
    alert("heelloo");
    
    }
    
    </script>
        2
  •  1
  •   Devsi Odedra    7 年前

    在输入中使用onchange,如下所示

    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    .switch {
      position: relative;
      display: inline-block;
      width: 60px;
      height: 34px;
    }
    
    .switch input { 
      opacity: 0;
      width: 0;
      height: 0;
    }
    
    .slider {
      position: absolute;
      cursor: pointer;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: #ccc;
      -webkit-transition: .4s;
      transition: .4s;
    }
    
    .slider:before {
      position: absolute;
      content: "";
      height: 26px;
      width: 26px;
      left: 4px;
      bottom: 4px;
      background-color: white;
      -webkit-transition: .4s;
      transition: .4s;
    }
    
    input:checked + .slider {
      background-color: #2196F3;
    }
    
    input:focus + .slider {
      box-shadow: 0 0 1px #2196F3;
    }
    
    input:checked + .slider:before {
      -webkit-transform: translateX(26px);
      -ms-transform: translateX(26px);
      transform: translateX(26px);
    }
    
    /* Rounded sliders */
    .slider.round {
      border-radius: 34px;
    }
    
    .slider.round:before {
      border-radius: 50%;
    }
    </style>
    </head>
    <body>
    
    <h2>Toggle Switch</h2>
    
    <label class="switch" >
      <input type="checkbox" onchange="function1()">
      <span class="slider"></span>
    </label>
    
    
    
    </body>
    
    
    
    
    </html> 
    <script>
    function function1(){
    alert("heelloo");
    
    }
    
    </script>
        3
  •  1
  •   Carsten Løvbo Andersen Dan Nemtanu    7 年前

    将函数下移到输入

    <input type="checkbox" onclick="function1()">
    

    工作演示

    function function1() {
      alert("heelloo");
    
    }
    .switch {
      position: relative;
      display: inline-block;
      width: 60px;
      height: 34px;
    }
    
    .switch input {
      opacity: 0;
      width: 0;
      height: 0;
    }
    
    .slider {
      position: absolute;
      cursor: pointer;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: #ccc;
      -webkit-transition: .4s;
      transition: .4s;
    }
    
    .slider:before {
      position: absolute;
      content: "";
      height: 26px;
      width: 26px;
      left: 4px;
      bottom: 4px;
      background-color: white;
      -webkit-transition: .4s;
      transition: .4s;
    }
    
    input:checked+.slider {
      background-color: #2196F3;
    }
    
    input:focus+.slider {
      box-shadow: 0 0 1px #2196F3;
    }
    
    input:checked+.slider:before {
      -webkit-transform: translateX(26px);
      -ms-transform: translateX(26px);
      transform: translateX(26px);
    }
    
    
    /* Rounded sliders */
    
    .slider.round {
      border-radius: 34px;
    }
    
    .slider.round:before {
      border-radius: 50%;
    }
    <h2>Toggle Switch</h2>
    
    <label class="switch">
      <input type="checkbox" onclick="function1()">
      <span class="slider"></span>
    </label>
    
    
    
    
    </html>
    <script>
    </script>