代码之家  ›  专栏  ›  技术社区  ›  Mechlar

Unbind mouseup?

  •  1
  • Mechlar  · 技术社区  · 15 年前

    我正在尝试从元素中解除mouseup事件的绑定。我试过以下方法,但都不管用。

    $('#myElm').unbind('mouseup');
    $('#myElm').unbind('onmouseup');
    $('#myElm').unbind('click');
    

    如何取消绑定使用$('myelm').mouseup(函数(…))分配的事件????

    编辑:添加完整代码


    cacheBgArea.mouseup(function(){
          var $cursorInElm = $(cacheBgArea.selectedText().obj);
          var selectFontSize = parseInt($cursorInElm.css('fontSize')), selectFontFace = $cursorInElm.css('fontFamily');
          $fontSizeSlider.slider('value', selectFontSize);
    
          $chooseFontFace.find('option').each(function(){
             var $this = $(this);
             if ($this.val() == selectFontFace) {
                $this.attr('selected', true);
                return false;
             }
          });
          log('font weight: ' + $cursorInElm.css('fontWeight'));
          if ($cursorInElm.css('fontWeight') == 'bold' || $cursorInElm.css('fontWeight') == 401) {
             $boldCheckbox.attr('checked', true).change();
          } else {
             $boldCheckbox.attr('checked', false).change();
          }
    
          var objText = cacheBgArea.selectedText();
          if (objText.obj.nodeName == 'a' || objText.obj.nodeName == 'A') {
             $cursorInElm = $(objText.obj)
             var elmsHref = $cursorInElm.attr('href');
             if (elmsHref && elmsHref != '#') {
                $enterOwnLink.val(elmsHref).show();
                $switchToPage.show();
                $chooseLinkPage.hide();
                $chooseLinkTitle.html('Enter a Web Address');
             } else if ($cursorInElm.attr('linkPageId')) {
                $chooseLinkPage.find('option').each(function(){
                   var $this = $(this);
                   if ($this.val() == $cursorInElm.attr('linkPageId')) {
                      $this.attr('selected', true);
                      return false;
                   }
                });
                $enterOwnLink.hide();
                $switchToPage.hide();
                $chooseLinkPage.show();
                $chooseLinkTitle.html('Choose a Page');
             }
          } else {
             $('#noneLink').attr('selected', true);
             $enterOwnLink.hide();
             $switchToPage.hide();
             $chooseLinkPage.show();
             $chooseLinkTitle.html('Choose a Page');
          }
       });
    

    我已经证实cachebgrain确实定义了。是的,在调用解除绑定之前绑定事件。这是解除绑定。(log只是console.log();的简写)

    log('cacheBgArea.length: ' + cacheBgArea.length);
    cacheBgArea.unbind('mouseup');//TODO: fix this, not unbinding...
    
    1 回复  |  直到 15 年前
        1
  •  8
  •   Nick Craver    15 年前

    这应该是有效的:

    $('#myElm').unbind('mouseup');
    

    你能发布完整的绑定代码吗?另外,你确定这是在运行吗 之后 这个 .mouseup() 跑?

    .mouseup(func) 是一个快捷方式 .bind('mouseup', func) 所以取消绑定的匹配是 .unbind('mouseup') (注意,这是解除绑定 全部的 mouseup 处理程序,而不仅仅是匿名函数,如果要删除特定的处理程序,则需要一个命名函数)。