代码之家  ›  专栏  ›  技术社区  ›  Andrew Rumm

使用jQuery阻止onclick操作

  •  31
  • Andrew Rumm  · 技术社区  · 16 年前

    <a href="#" onclick="alert('panic!')">Let's panic</a>
    <a href="#" onclick="alert('panic!')" disabled="disabled">I can't panic no more</a>
    

    $('a[disabled]').click(function(e){
      e.stopPropagation();
      return false;
    });
    

    <html><head><script type='text/javascript' src='jquery-1.3.2.js'></script></head>
    <body>
    <a href='#' onclick="alert('HA-ha!')" disabled="disabled" class="disabled">TEST</a>
    <script type="text/javascript">
        $('a[disabled], a.disabled').click(function(e){
            console.log('override?');
            e.stopImmediatePropagation();           
                e.preventDefault();
            e.stopPropagation();
            return false;       
        });
    </script>
    </body></html>
    
    5 回复  |  直到 16 年前
        1
  •  55
  •   a_m0d    13 年前

    jQuery不会解决这个OOTB问题。它可以帮助,但没有一个 stopPropagation , stopImmediatePropagation , preventDefault , return false 如果你只是将它们附加到元素上,它就会工作。你需要 以(权力)否决

    “不删除onclick操作” null onclick 禁用锚点的属性):

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <title>Disable clicks</title>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    </head>
    <body>
      <a href="#" onclick="alert('panic!')">Let's panic</a>
      <a href="#" onclick="alert('panic!')" disabled="disabled">I can't panic no more</a>
    
      <script>
      $('a[onclick]').each(function(){
        $(this).data('onclick', this.onclick);
    
        this.onclick = function(event) {
          if($(this).attr('disabled')) { // HERE
            return false;
          };
    
          $(this).data('onclick').call(this, event || window.event);
        };
      });
      </script>
    </body>
    </html>
    

    Demo here.

    方法是覆盖内联点击处理程序( 返回假

    .removeAttr('disabled')

        2
  •  9
  •   rfunduk    16 年前

    disabled rel='disabled' 相反。

    $('a[rel="disabled"]').click( function() { return false; } );
    

    更新

    alert 因为它实际上是内联在标记中的!我从来没有这样做,所以没有注意到。将点击处理程序从标记中取出,并首先在jQuery中执行,这样你就可以执行以下操作:

    $('a').click( function() {
      if( $(this).attr('rel') == 'disabled' ) { return; }
      // do stuff here when not disabled
      return false; // so your '#' href doesn't get used
    } );
    
        3
  •  6
  •   Doug Neiner    16 年前

    问题是jQuery按顺序添加事件。要阻止其他事件,你需要阻止的事件必须到来 之后

    <a href='#' onclick="alert('HA-ha!')" class="disabled">TEST</a>
    <a href='#' onclick="alert('HA-ha!')">TEST</a>
    <script type="text/javascript">
        $('a').each(function(){
            // Cache event
            var existing_event = this.onclick;
    
            // Remove the event from the link
            this.onclick = null;
    
            // Add a check in for the class disabled
            $(this).click(function(e){ 
                if($(this).hasClass('disabled')){
                    e.stopImmediatePropagation();
                    e.preventDefault();
                }                
            });
    
            // Reattach your original onclick, but now in the correct order
            // if it was set in the first place
            if(existing_event) $(this).click(existing_event);
        });
    </script>
    

    $('a#whatever').addClass('disabled') $('a#whatever').removeClass('disabled')

        4
  •  1
  •   tvanfosson    16 年前

    jQuery添加的任何点击处理程序似乎都会在标记中添加的点击处理程序之后触发。我的解决方案是使用jQuery而不是在标记中应用点击处理程序,但你可能对代码没有足够的控制来做到这一点。如果你这样做,那么就不要将点击处理程序应用于禁用类的锚定标签(是的,我会使用类而不是不合适的属性)。如果您无法控制标记,则可能需要使用jQuery替换点击处理程序,并将其存储以供以后重新应用。

       $(function() {
          $('a.disabled').each( function() {
             var $this = $(this);
             var click = $this.attr('onclick');
             if (click) {
                 $this.data('click',click);
                 // add return false to prevent default action
                 $this[0].onclick =  function() { return false; };
             }
          });
    
          $('#restoreClick').click( function() {
              $('a.disabled').each( function() {
                  var $this = $(this);
                  $this.removeClass('disabled');
                  var click = $this.data('click');
                  if (click) {
                      $this[0].onclick = click;
                  }
              });
          });
       });
    

    测试方法:

    <div>
        <a href="#" onclick="alert('panic!')">Let's panic</a>
        <a href="#" onclick="alert('panic!')" class="disabled">I can't panic no more</a>
    </div>
    <div>
        <input type="button" id="restoreClick" value="Restore" />
    </div>
    
        5
  •  0
  •   MrBojangles    14 年前

    <a href="javascript:alert('panic!')" >Let's panic</a>
    <a href="javascript:alert('panic!')" disabled="disabled">I can't panic no more</a>
    
    $('a').click(function(e){
        if($(this).attr('disabled')) e.preventDefault();
    });