如何用jQuery替换事件处理程序? [英] How do I replace an event handler with jQuery?
问题描述
我有一个使用AJAX导航的网站。我有两个页面,我使用点击和拖动功能
I have a site that uses AJAX to navigate. I have two pages that I use a click and drag feature using
$(".myDragArea").mousedown(function(){
do stuff...
mouseDrag = true; // mouseDrag is global.
});
$("body").mousemove(function(){
if (mouseDrag) {
do stuff...
}
});
$("body").mouseup(function(){
if (mouseDrag) {
do stuff...
mouseDrag = false;
}
});
我只需输入,所以请原谅任何偶然的语法错误。该站点的两个部分使用几乎相同的代码,唯一的区别是 $(body)。mouseup()
函数内部。但是,如果我访问第一部分,然后导航到第二部分,则在mouseup上运行的代码不会更改。我已经使用firebug逐步执行代码,当 $(body)时没有错误或抛出。当第二部分加载时运行mouseup()
。
I just type that out, so excuse any incidental syntax errors. Two parts of the site use almost identical code, with the only difference being what is inside the $("body").mouseup()
function. However, if I access the first part, then navigate to the second part, the code that runs on mouseup doesn't change. I have stepped through the code with firebug, and no errors or thrown when $("body").mouseup()
is run when the second part loads.
所以,当我运行 $(body)时,为什么事件处理程序不会改变.mouseup()
第二次?
So, why doesn't the event handler change when I run $("body").mouseup()
the second time?
推荐答案
调用 $(body)。mouseup(function)
将添加一个事件处理程序。
您需要通过编写 $(body)来删除现有的处理程序.unbind(' mouseup');
。
Calling $("body").mouseup(function)
will add an event handler.
You need to remove the existing handler by writing $("body").unbind('mouseup');
.
这篇关于如何用jQuery替换事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!