简体   繁体   English

单击动作在第二次单击时起作用

[英]Click Actions works on second click

This code works properly on first click. 此代码在首次点击时即可正常运行。 When repeated its works only on second click on same element. 当重复时,仅在第二次单击同一元素时才起作用。 HTML 的HTML

<div id="monthly-table">
<a href="#" class="monthly active">Monthly</a>
<a href="#" onclick="subscriptionTable(this)" class="yearly">Yearly</a>
<h1>Monthly</h1></div><div id="yearly-table" style="display:none">
<a href="#" onclick="subscriptionTable(this)" class="monthly">Monthly</a>
<a href="#" class="yearly active">Yearly</a>
<h1>Yearly</h1></div>

SCRIPT 脚本

function subscriptionTable(el) {
   $(el).on('click', function() {
       if ($('#yearly-table').is(':hidden')) {
          $('#yearly-table').show();
          $('#monthly-table').hide();
        } else {
         $('#yearly-table').hide();
         $('#monthly-table').show();
       }
      return false;
   });
};

Instead of using the inline click handler to register the jQuery click handler, use jQuery dom ready handler to register the click handler 与其使用嵌入式单击处理程序注册jQuery单击处理程序,不如使用jQuery dom ready处理程序注册单击处理程序。

 jQuery(function($) { $('.period').on('click', function() { var $target = $($(this).attr('href')).show(); $('.target').not($target).hide(); return false; }); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="monthly-table" class="target"> <a href="#" class="monthly active">Monthly</a> <a href="#yearly-table" class="yearly period">Yearly</a> <h1>Monthly</h1> </div> <div id="yearly-table" class="target" style="display:none"> <a href="#monthly-table" class="monthly period">Monthly</a> <a href="#" class="yearly active">Yearly</a> <h1>Yearly</h1> </div> 

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

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