<tfoot id='owRFc'></tfoot>
  1. <legend id='owRFc'><style id='owRFc'><dir id='owRFc'><q id='owRFc'></q></dir></style></legend>
      <bdo id='owRFc'></bdo><ul id='owRFc'></ul>
    <i id='owRFc'><tr id='owRFc'><dt id='owRFc'><q id='owRFc'><span id='owRFc'><b id='owRFc'><form id='owRFc'><ins id='owRFc'></ins><ul id='owRFc'></ul><sub id='owRFc'></sub></form><legend id='owRFc'></legend><bdo id='owRFc'><pre id='owRFc'><center id='owRFc'></center></pre></bdo></b><th id='owRFc'></th></span></q></dt></tr></i><div id='owRFc'><tfoot id='owRFc'></tfoot><dl id='owRFc'><fieldset id='owRFc'></fieldset></dl></div>

      <small id='owRFc'></small><noframes id='owRFc'>

    1. 替代支持 eventData 的 jQuery 的 .toggle() 方法?

      时间:2023-09-05

      <tfoot id='5vcTZ'></tfoot>

        <tbody id='5vcTZ'></tbody>
      <legend id='5vcTZ'><style id='5vcTZ'><dir id='5vcTZ'><q id='5vcTZ'></q></dir></style></legend>
        • <i id='5vcTZ'><tr id='5vcTZ'><dt id='5vcTZ'><q id='5vcTZ'><span id='5vcTZ'><b id='5vcTZ'><form id='5vcTZ'><ins id='5vcTZ'></ins><ul id='5vcTZ'></ul><sub id='5vcTZ'></sub></form><legend id='5vcTZ'></legend><bdo id='5vcTZ'><pre id='5vcTZ'><center id='5vcTZ'></center></pre></bdo></b><th id='5vcTZ'></th></span></q></dt></tr></i><div id='5vcTZ'><tfoot id='5vcTZ'></tfoot><dl id='5vcTZ'><fieldset id='5vcTZ'></fieldset></dl></div>

              <small id='5vcTZ'></small><noframes id='5vcTZ'>

                <bdo id='5vcTZ'></bdo><ul id='5vcTZ'></ul>

                本文介绍了替代支持 eventData 的 jQuery 的 .toggle() 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                .toggle() 方法的 jQuery 文档 声明:

                提供 .toggle() 方法是为了方便.手动实现相同的行为相对简单,如果 .toggle() 内置的假设被证明是有限的,这可能是必要的.

                The .toggle() method is provided for convenience. It is relatively straightforward to implement the same behavior by hand, and this can be necessary if the assumptions built into .toggle() prove limiting.

                .toggle 中内置的假设已证明对我当前的任务有限制,但文档并未详细说明如何实现相同的行为.我需要将 eventData 传递给提供给 toggle() 的处理函数,但似乎只有 .bind() 会支持这一点,而不是.toggle().

                The assumptions built into .toggle have proven limiting for my current task, but the documentation doesn't elaborate on how to implement the same behavior. I need to pass eventData to the handler functions provided to toggle(), but it appears that only .bind() will support this, not .toggle().

                我的第一个倾向是使用对单个处理函数全局的标志来存储点击状态.换句话说,而不是:

                My first inclination is to use a flag that's global to a single handler function to store the click state. In other words, rather than:

                $('a').toggle(function() {
                  alert('odd number of clicks');
                }, function() {
                  alert('even number of clicks');
                });
                

                这样做:

                var clicks = true;
                $('a').click(function() {
                  if (clicks) {
                    alert('odd number of clicks');
                    clicks = false;
                  } else {
                    alert('even number of clicks');
                    clicks = true;
                  }
                });
                

                我没有测试过后者,但我怀疑它会起作用.这是做这种事情的最好方法,还是我错过了更好的方法?

                I haven't tested the latter, but I suspect it would work. Is this the best way to do something like this, or is there a better way that I'm missing?

                推荐答案

                似乎是一个合理的方法......我只是建议你使用 jQuery 的 数据存储 实用程序,而不是引入一个额外的变量(如果你想跟踪一大堆链接,这可能会让人头疼).所以根据你的例子:

                Seems like a reasonable way to do it... I'd just suggest that you make use of jQuery's data storage utilities rather than introducing an extra variable (which could become a headache if you wanted to keep track of a whole bunch of links). So based of your example:

                $('a').click(function() {
                  var clicks = $(this).data('clicks');
                  if (clicks) {
                    alert('odd number of clicks');
                  } else {
                    alert('even number of clicks');
                  }
                  $(this).data("clicks", !clicks);
                });
                

                这篇关于替代支持 eventData 的 jQuery 的 .toggle() 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何删除带有匿名函数的 addEventListener 的EventLi 下一篇:检测到双击事件时需要取消单击/鼠标事件

                相关文章

                最新文章

                1. <small id='ULnZD'></small><noframes id='ULnZD'>

                    <legend id='ULnZD'><style id='ULnZD'><dir id='ULnZD'><q id='ULnZD'></q></dir></style></legend>

                    <tfoot id='ULnZD'></tfoot>
                    • <bdo id='ULnZD'></bdo><ul id='ULnZD'></ul>
                    <i id='ULnZD'><tr id='ULnZD'><dt id='ULnZD'><q id='ULnZD'><span id='ULnZD'><b id='ULnZD'><form id='ULnZD'><ins id='ULnZD'></ins><ul id='ULnZD'></ul><sub id='ULnZD'></sub></form><legend id='ULnZD'></legend><bdo id='ULnZD'><pre id='ULnZD'><center id='ULnZD'></center></pre></bdo></b><th id='ULnZD'></th></span></q></dt></tr></i><div id='ULnZD'><tfoot id='ULnZD'></tfoot><dl id='ULnZD'><fieldset id='ULnZD'></fieldset></dl></div>