取消绑定鼠标?

发布于 2024-09-14 13:56:54 字数 2460 浏览 4 评论 0原文

我正在尝试从元素中解除 mouseup 事件的绑定。我尝试过以下方法,但没有一个有效。

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

如何取消绑定使用 $('#myElm').mouseup(function({...}); 分配的事件 ???

编辑:添加完整代码


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');
      }
   });

我已经验证了确实定义了cacheBgArea是的,事件在取消绑定之前被绑定。这就是取消绑定(log 只是我对 console.log(); 的简写)。

log('cacheBgArea.length: ' + cacheBgArea.length);
cacheBgArea.unbind('mouseup');//TODO: fix this, not unbinding...

I am trying to unbind the mouseup event from an element. I have tried the following but none are working.

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

How do you unbind an event assigned using $('#myElm').mouseup(function({...}); ???

Edit: Adding full code


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');
      }
   });

I have verified that cacheBgArea in indeed defined. Yes, the event is bound before the unbind is called. This is the unbind. (log is just my shorthand for console.log();)

log('cacheBgArea.length: ' + cacheBgArea.length);
cacheBgArea.unbind('mouseup');//TODO: fix this, not unbinding...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぶ宁プ宁ぶ 2024-09-21 13:56:54

这应该有效:

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

您可以发布完整的绑定代码吗?另外,您确定这是在 .mouseup() 之后运行吗?代码>跑了?

.mouseup(func).bind('mouseup', func) 因此匹配解除绑定是 .unbind('mouseup') (请注意,这会解除 全部 的绑定code>mouseup 处理程序,而不仅仅是匿名函数,如果您想删除特定的处理程序,您将需要一个命名函数)。

This should be working:

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

Can you post your complete bind code? Also, are you sure this is running after the .mouseup() ran?

.mouseup(func) is a shortcut for .bind('mouseup', func) so the match unbind is .unbind('mouseup') (note this unbinds all of the mouseup handlers, not just the anonymous function, you'll need a named function if you want to remove a specific handler).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文