简体   繁体   English

Jquery-toggle单击功能,单击不起作用。 但它可以双击

[英]Jquery-toggle on click function ,does not work on single click. But it works on double click

Hi, im having some problem with toggle functionality,its working only on double click not on single click,so can anyone help me on this . 嗨,我在切换功能方面遇到了一些问题,它只能在双击上工作,而不能在单击上工作,所以任何人都可以帮我解决这个问题。

Below is my HTML code: 以下是我的HTML代码:

    <div class="search_result_per_page">
    <dl class="selectcsdd perpage">
    <dt>
    <a href="javascript:void(0);" rel="nofollow">
    <span id="dropdownArrow" class="close">Items per page</span>
    </a>
   </dt>
   <dd>
   <ul style="display: none;">
  <li>
  <li>
  <li>
  </ul>
  </dd>
  </dl>
  </div>

I want to toggle UL when I click on span id="dropdownArrow" . 当我单击span id =“ dropdownArrow”时,我想切换UL。 But it only toggles on the double click. 但它仅在双击上切换。 The Jquery code written for this is as follows. 为此编写的Jquery代码如下。

    $(document).ready(function($)
       {
          $(".selectcsdd.perpage dt a").click(function() { 
                $(".selectcsdd.perpage dd ul").toggle(function(){               
            var arrow = $(".selectcsdd.perpage #dropdownArrow").attr('class');
                if(arrow=='close'){
                $(".selectcsdd.perpage #dropdownArrow").attr('class','open');
            }
            else if(arrow=='open'){
                $(".selectcsdd.perpage #dropdownArrow").attr('class','close');
            }
        });
    });
    $(".selectcsdd.sortby dt a").click(function() {
        $(".selectcsdd.sortby dd ul").toggle(function(){
            var arrow = $(".selectcsdd.sortby #dropdownArrow").attr('class');
            if(arrow=='close'){
                $(".selectcsdd.sortby #dropdownArrow").attr('class','open');
            }
            else if(arrow=='open'){
                $(".selectcsdd.sortby #dropdownArrow").attr('class','close');
            }
        });
    });

    $(document).bind('click', function(e) {
        var $clicked = $(e.target);
        if (! $clicked.parents().hasClass("open"))
            $(".selectcsdd.perpage dd ul").hide();
            $(".selectcsdd.sortby dd ul").hide();
    });
});

Any idea? 任何想法?

You are using .click() and .bind('click') on the same element which which seems to be causing an issue. 您正在同一元素上使用.click().bind('click') ,这似乎引起了问题。 If you remove the .bind() it works fine 如果删除.bind()它将正常工作

Try this: 尝试这个:

$(document).ready(function($) {

    $(".selectcsdd.perpage dt a").click(function(e) { 
        $(".selectcsdd.perpage dd ul").toggle(function(){               
            var arrow = $(e.target).attr('class');
            if(arrow=='close'){
                $(".selectcsdd.perpage #dropdownArrow").attr('class','open');
            }
            else if(arrow=='open'){
                $(".selectcsdd.perpage #dropdownArrow").attr('class','close');
            }
            if (! e.class == 'open'){
                $(".selectcsdd.perpage dd ul").hide();
                $(".selectcsdd.sortby dd ul").hide();
            }
        });
    });
    $(".selectcsdd.sortby dt a").click(function() {
        $(".selectcsdd.sortby dd ul").toggle(function(){
            var arrow = $(".selectcsdd.sortby #dropdownArrow").attr('class');
            if(arrow=='close'){
                $(".selectcsdd.sortby #dropdownArrow").attr('class','open');
            }
            else if(arrow=='open'){
                $(".selectcsdd.sortby #dropdownArrow").attr('class','close');
            }
        });
    });
});

Having the click event bound to the document and the link was causing the error. 单击事件绑定到文档和链接导致了错误。 The link would catch the event and leave the document with nothing. 该链接将捕获该事件,而使文档一无所有。 Since the event is only concerned about the links why not just handle everything when the event occurs? 由于事件仅关注链接,为什么在事件发生时不仅仅处理所有内容?

--lol 20 minutes late-- -大声晚20分钟-

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

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