简体   繁体   English

无法使切换功能正常工作

[英]can't get the toggle function to work

I am new to jQuery and am experimenting with some plugins. 我是jQuery新手,正在尝试一些插件。 I found one called jQuery Balloon. 我找到了一个叫做jQuery Balloon的东西。 When I add my script and run the code the bubble pops up but won't go away. 当我添加脚本并运行代码时,气泡弹出,但不会消失。

Here is my code: 这是我的代码:

  <script type="text/javascript" src="/js/jquery.js"></script>
  <script type="text/javascript" src="/js/jquery.balloon.min.js"></script>

Here is my jQuery code. 这是我的jQuery代码。 I omitted the document.ready because I have that at the top of my document already. 我省略了document.ready因为我已经在文档的顶部了。

 $("a").click(function(event){
   $(this).showBalloon().toggle();
    event.preventDefault();

How do I get the balloon popup when the user clicks the link and have it go away when they click away from it? 当用户单击链接时,如何获得气球弹出框,当用户单击链接时,该气球弹出框消失?

You'll need to track the popup's state, since the .toggle(fn1, fn2) function is now deprecated : 由于现在不赞成使用 .toggle(fn1, fn2)函数.toggle(fn1, fn2)您需要跟踪弹出窗口的状态:

$('a').on('click', function(ev) {
    var $this = $(this);
    var state = !$this.data('balloon');  // use .data, initially undefined
    if (state) {
        $this.hideBalloon();
    } else {
        $this.showBalloon();
    }
    $this.data('balloon', state);
    ev.preventDefault();
});

Note that this will maintain a separate state for each a link. 请注意,这将会为每一个单独的状态a链接。

$('a').on('click', ( function balloonState(){
  var state = true;

  return function(ev) {
      var $this = $(this);

      if (state) {
          $this.showBalloon();
      } else {
          $this.hideBalloon();
      }

      state = !state;

      ev.preventDefault();
  } 
}() ) );

Alnitak's answer is functionally perfect as far as the question goes, but you can achieve the same with simple native closures (without having to use $.fn.data ), the advantages being: 就问题而言,Alnitak的答案在功能上是完美的,但是您可以使用简单的本机闭包(无需使用$.fn.data )来实现相同的$.fn.data ,其优点是:

  • Saving on code execution by avoiding jQuery's DOM-associated data binding and only declaring state once 通过避免jQuery的DOM相关数据绑定而只声明一次state节省了代码执行时间
  • Avoid needlessly saving state in the DOM, where other code can tamper with it 避免在DOM中不必要地保存状态,以免其他代码对其进行篡改

EDIT: As Alnitak pointed out in the comments, an immediately invoked closure would only create one state variable for all <a> s. 编辑:正如Alnitak在评论中指出的那样,立即调用的闭包只会为所有 <a>创建一个state变量。 Assuming that's not what's desired, we can use jQuery's each to create a closure for each <a> : 假设这不是期望的,我们可以使用jQuery的each为每个<a>创建一个闭包:

$('a').each( function balloonState(){
  var state = true;
  var $this = $( this );

  $this.on( 'click', function(ev) {
      if (state) {
          $this.showBalloon();
      } else {
          $this.hideBalloon();
      }

      state = !state;

      ev.preventDefault();
  } );
} );

EDIT 2: The method above has the caveat that it will create a new function for each link on the page at runtime, which is very wasteful in terms of memory management and speedy initialization. 编辑2:上述方法有一个警告,那就是它将在运行时为页面上的每个链接创建一个新函数,这在内存管理和快速初始化方面非常浪费。 The plugin homepage uses the old jQuery toggle method to get around the problem of when to execute hideBalloon and showBalloon , but this was deprecated in jQuery 1.9 . 插件主页使用旧的jQuery toggle方法来解决何时执行hideBalloonshowBalloon ,但这在jQuery 1.9中已弃用 Overall I'd recommend using Alnitak's method . 总的来说,我建议使用Alnitak的方法

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

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