取消绑定鼠标?
我正在尝试从元素中解除 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该有效:
您可以发布完整的绑定代码吗?另外,您确定这是在
.mouseup() 之后运行吗?代码>
跑了?
.mouseup(func)
是.bind('mouseup', func)
因此匹配解除绑定是.unbind('mouseup')
(请注意,这会解除 全部 的绑定code>mouseup 处理程序,而不仅仅是匿名函数,如果您想删除特定的处理程序,您将需要一个命名函数)。This should be working:
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 themouseup
handlers, not just the anonymous function, you'll need a named function if you want to remove a specific handler).